home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / ANSIshellƒ / os_mac_eEdit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-18  |  87.2 KB  |  3,041 lines  |  [TEXT/R*ch]

  1. /* os_mac_eEdit.c 
  2.  * 8Apr92  e
  3.  */
  4.  
  5. #ifdef THINK_C
  6. #include <MacHeaders>
  7. #else
  8. #ifdef __MWERKS__
  9. #else
  10. "Unknown compiler!"
  11. #endif
  12. #endif
  13.  
  14. #include "os_mac_eEdit.h"
  15.  
  16. /* an editor for text files by e
  17.    questions/comments via Internet <e@Flavors.COM> */
  18. /* Copyright © e, 1992. All rights reserved.
  19.     Developed using THINK C 5.0.1 for use with Gambit Scheme.
  20.     This code may be freely distributed as long as this notice remains.
  21.    based upon CEditor.C...
  22.     Copyright © BRH Toolsmith, 1992. All rights reserved.
  23.     Developed using THINK C 4.0.2 and Symantec's OOP libraries.
  24.     Portions of this code courtesy Symantec, Inc.
  25.     This code may be freely distributed as long as this notice remains. If you wish
  26.     to distribute modifications to these classes, please make a copy of the class
  27.     and implement your changes there. That's the beauty of OOP!
  28. since:
  29. a) I was not using Symantec's OOP, and
  30. b) CEditor.C had (due to the dependence on the Symantec libraries) a serious limitation,
  31.  to wit a limit of 32,767 pixels of document height (about 3000 lines of text),
  32. eEdit.c is a recasting of CEditor.C in vanilla C with very significant enhancements and fixes.
  33. */
  34.  
  35. /* 10Dec92  e  -- to do:
  36. - better out-of-memory error handling
  37. */
  38.  
  39. /* for arrow keys which ~work like ThinkC editor instead of Apple standard
  40. #define THINK_ARROWS (1)
  41. */
  42. /* for RETURN which auto-tabs like ThinkC editor - not good with Gambit
  43. 16Dec92  e  -- dont turn this on; it doesn't work anymore!
  44. #define THINK_RETURN (1)
  45. */
  46. /* to allow chars > 127 in the text... */
  47. #define CHAR8_OK (1)
  48.  
  49. /* to make Kill (DEL FWD) copy (c-K) vs. delete -- 24Aug94 e */
  50. #define Kill_Deletes (1)
  51.  
  52. /* eEdit documentation...
  53.  
  54.     Limitations
  55.         text size is limited by...
  56.             memory space
  57.               = ~200 + text + (#lines + #style-runs + MIN_LINES + MIN_RUNS) * 4
  58.             32K lines max
  59.             32K style runs max
  60.           eEdit's been tested with files several hundred K in size
  61.           there is no pixel height limit (which constrains many editors to ~3000 lines)
  62.         exactly two styles per text
  63.  
  64.     eTeKey()
  65.         EXTENDED KEYBOARD:
  66.             F1 - F4 - Undo, Cut, Copy, Paste
  67.             F5 - F15 - function keys (not implemented)
  68.             KeyDel - delete forward, erase character after cursor position
  69.             [these next four do not move the insert point, just scroll]
  70.             KeyHome - scrolls text to the top of the screen
  71.             KeyEnd - scrolls text to the bottom of the screen
  72.             KeyPageUp - scrolls text up a page
  73.             KeyPageDown - scrolls text down a page
  74.  
  75.         ALL KEYBOARDS
  76.             LEFT_ARROW - move cursor to the left one character
  77.             RIGHT_ARROW - move cursor to the right one character
  78.             UP_ARROW - move cursor up one line
  79.             DOWN_ARROW - move cursor down one line
  80.             DELETE - erase character before cursor position
  81.             RETURN/ENTER - insert new line and move to left margin (insert a RETURN)
  82.             TAB - insert TAB character; displayed as N spaces
  83.             0x20 - 0xFF - range of valid text characters
  84.  
  85.       Arrow keys can be modified with shift, option, and command
  86.         shift - extend selection per Apple standard (optionally ThinkC mode)
  87.         option - left/right by word, may be used with shift key to extend by word
  88.         option - up/down by page, may be used with shift key to extend by page
  89.         option+shift up/down arrows make the start/end of the selection the active end
  90.         command - left/right: start/end of line, up/down: start/end of text
  91.                     may be used with shift key to extend selection
  92.  
  93.     *** NOTE: 
  94.     *** command-arrow keys will work ONLY IF the main event loop
  95.     *** lets command keys not associated with menu items pass thru to eEdit
  96.  
  97.     eTeClick()
  98.         Mouse clicks
  99.             single - position insert point
  100.             double - select word
  101.             triple - select line
  102.           dragging after one of these extends selection
  103.             by character, word, line repectively; anchor point is original click,
  104.             active end is the other end of the selection
  105.           after drag, active end can be moved with shift clicks or shift arrow keys
  106.           anchor and active ends of selection can be swapped
  107.             with option+shift+click, or option+shift+up/down arrow keys
  108.             
  109.     scrolling: holding down command, shift, option, and control keys
  110.                 speeds up scrolling when mousing in the arrows of the scroll bar
  111.                 speed is 2^^count lines per iteration,
  112.                  where count is a count of the pressed keys
  113. */
  114.  
  115.  
  116. #define OPTION_SPACE    ' '                /* typed as OPTION <SPACE> - used for tabs */
  117. #define EMPTY_PTR        ((Ptr )2L)        /* valid but nonsense ptr used for Munger */
  118. #define PTL(a)           *((long *)&(a))    /* coerce Point or ChPos type to long */
  119.  
  120. #define        Abs(x)            ((x) < 0 ? -(x) : (x))
  121. #define        Max(x, y)        ((x) > (y) ? (x) : (y))
  122. #define        Min(x, y)        ((x) < (y) ? (x) : (y))
  123.  
  124. #define CARET_ON FALSE
  125. #define CARET_OFF TRUE
  126.  
  127. EventRecord            gLastMouseUp;
  128. EventRecord            gLastMouseDown;
  129. long                gMaxSleep;
  130.  
  131. static short         gClicks;
  132. static RgnHandle     gUtilRgn;
  133. static eRec          **gLastViewHit;
  134. static ChPos        zeroPos = { 0, 0 };
  135. /*
  136. #define    MAXINT        32767
  137. static Rect            gMobyRect = { -MAXINT, -MAXINT, MAXINT, MAXINT };
  138. */
  139. TextStyle dfltStylNormal =
  140. { monaco, normal,          0, 9, { 65536, 65536, 65536 } /* RGBColor Black */ };
  141. TextStyle dfltStylHilite =
  142. { monaco, bold+condense,   0, 9, { 65536, 65536, 65536 } /* RGBColor Black */ };
  143.  
  144.  
  145. static void eTeEdGuts( eRec **hE, Ptr textPtr, long numChars,
  146.                          Boolean hasReturn, Boolean show, short style );
  147. static void eTeFontChanged( eRec **hE );
  148. static void eTeHiliteRange( eRec **hE, register ChPos start, register ChPos stop );
  149. static void eTeAdjustScrollMax( eRec **hE );
  150. static void eTeCalibrate( eRec **hE );
  151. static short eTeOffsetToRun( eRec **hE, long anOffset );
  152. static short eTeRunToStyle( eRec **hE, short r );
  153. static ChPos eTePointHtoChPosH( eRec **hE, register Point aPt, register ChPos aPos );
  154. static ChPos eTePointToChPos( eRec **hE, register Point aPt );
  155. static Point eTeChPosToPoint( eRec **hE, register ChPos aPos );
  156. static void eTeDoHscroll( eRec **hE, short whichPart );
  157. static void eTeDoVscroll( eRec **hE, short whichPart );
  158. static Boolean eTeAutoScroll( eRec **hE, Point mouseLoc );
  159. static pascal void hSBarActionProc( ControlHandle macControl, short whichPart );
  160. static pascal void vSBarActionProc( ControlHandle macControl, short whichPart );
  161. /* obsolete...
  162. void     eTeSetFontNumber( eRec **hE, short aFontNumber );
  163. void     eTeSetFontName( eRec **hE, Str255 aFontName );
  164. void     eTeSetFontSize( eRec **hE, short aFontSize );
  165. */
  166. static short eTeUpdateLineStarts( eRec **hE, short firstLine );
  167. static void eTeKeyIns( eRec **hE, char c, short style );
  168.  
  169. /* the scrap  13Aug92  e  */
  170. static Handle eTeScrap;
  171. static long   eTeScrapLen;
  172. static long   eTeScrapCnt;
  173.  
  174. /* undo       13Aug92  e  */
  175.  
  176. typedef struct undoStuff
  177. {    unsigned char *undoTitle;            /* string for Edit menu */
  178.     void  (*undoProc)( eRec  **hE );    /* proc to do undo */
  179.     Handle    undoText;                    /* text that was wiped by last operation */
  180.     long    undoTxLen;                    /* size of undoText */
  181.     long    undoInLen;                    /* number of chars inserted since last delete */
  182.     eRec  **undoTeRec;                    /* eRec       of last operation */
  183.     long    undoStart;                    /* selStart   before last operation */
  184.     long    undoEnd;                    /* selEnd     before last operation */
  185.     Boolean undoDirty;                    /* dirty      before last operation */
  186.     Boolean undoKeyAccum;                /* can accumulate keystrokes */
  187.     Boolean undoDeleting;                /* some version of delete was last operation */
  188.     Boolean undoCarStaP;                /* TRUE if CaretChPos == selStart */
  189. } undoStuff;
  190.  
  191. static undoStuff eTeUndoStuff;
  192.  
  193. unsigned char *utUndo   = "\pUndo";
  194. unsigned char *utCopy   = "\pUndo Copy";
  195. unsigned char *utCut    = "\pUndo Cut";
  196. unsigned char *utPaste  = "\pUndo Paste";
  197. unsigned char *utClear  = "\pUndo Clear";
  198. unsigned char *utTyping = "\pUndo Typing";
  199. unsigned char *utInsert = "\pUndo Insert";
  200. unsigned char *utKill   = "\pUndo Kill";
  201.  
  202. unsigned char *rtCopy   = "\pRedo Copy";
  203. unsigned char *rtCut    = "\pRedo Cut";
  204. unsigned char *rtPaste  = "\pRedo Paste";
  205. unsigned char *rtClear  = "\pRedo Clear";
  206. unsigned char *rtTyping = "\pRedo Typing";
  207. unsigned char *rtInsert = "\pRedo Insert";
  208. unsigned char *rtKill   = "\pRedo Kill";
  209.  
  210. /* DeTabifyHandle takes a text handle,
  211.     and a size (which it assumes is less than or equal to the HandleSize)
  212.      removes all characters in the range 0..0x1f except RETURN (0x0d)
  213.      replacing TAB characters (0x09) with spaces according to tabstop
  214.      it makes two passes over the text,
  215.       and only one resize call to the memory manager
  216.      it returns the number of lines in count (passed by reference)
  217.      and an OsErr result  ( 0 == noErr )
  218. */
  219. short DeTabifyHandle( Handle h, long *size, long *count, short tabstop )
  220. { long old_size = *size;
  221.   long new_size = *size;
  222.   long line_count = 0;
  223.   register unsigned char *start = *(unsigned char **)h;
  224.   register unsigned char *end = &start[old_size-1];
  225.   register unsigned char *p = start;
  226.   register unsigned char *q = start;
  227.   register unsigned char c;
  228.   short error;
  229.   char lastch = *end;
  230.   
  231.   *end++ = '\0';
  232.   while ( p < end )
  233.   { c = *p++;
  234.     if ( c < ' ' )
  235.     { if ( c == '\r' )
  236.       { line_count++;
  237.         *q++ = c;
  238.         start = q;
  239.       }
  240.       else if ( c == '\t' )
  241.       { c = tabstop - ( ( q - start ) % tabstop);
  242.         *q++ = c;
  243.         c -= 1;
  244.         start -= c;
  245.         new_size += c;
  246.       }
  247.       else if ( p == end )
  248.       {  *q++ = lastch; /* the last char may be a control char */
  249.       }
  250.       else
  251.       { old_size -= 1;
  252.         new_size -= 1;
  253.       }
  254.     }
  255.     else
  256.       *q++ = c;
  257.   }
  258.   *count = line_count;
  259.   *size = new_size;
  260.   SetHandleSize( h, new_size );
  261.   error = LMGetMemErr(); // asm { move.w D0, error } /* error = MemError(); */
  262.   if ( error == noErr )
  263.   { end = *(unsigned char **)h;
  264.     p = &end[new_size];
  265.     *--p = lastch;
  266.     start = &end[old_size-1];
  267.     while ( p > end )
  268.     { c = *--start;
  269.       if ( c < ' ' && c != '\r' )
  270.         while ( c-- ) *--p = ' ';
  271.       else
  272.         *--p = c;
  273.     }
  274.   }
  275.   else
  276.   { /* cleanup the buffer? */
  277.   }
  278.   return error;
  279. }
  280.  
  281. static void undoBugNi( eRec **hE )
  282. {
  283.     #pragma unused( hE )
  284.     SysBeep(3); SysBeep(10); SysBeep(3);
  285. }
  286.  
  287. static ControlActionUPP hSBarActionProcUPP; /* 04Jan95 e */
  288. static ControlActionUPP vSBarActionProcUPP; /* 04Jan95 e */
  289.  
  290. void eTeInit( void )
  291. {
  292.     gUtilRgn = NewRgn();
  293.     gClicks = 0;
  294.     /* 13Aug92  e  */
  295.     eTeScrap = NewHandle(0);
  296.     eTeScrapLen = 0;
  297.     eTeScrapCnt = (InfoScrap())->scrapCount - 1;
  298.     eTeGetScrap();
  299.     /* */
  300.     eTeUndoStuff.undoProc = undoBugNi;
  301.     eTeUndoStuff.undoTitle = utUndo;
  302.     eTeUndoStuff.undoText = NewHandle(0);
  303.     eTeUndoStuff.undoTeRec = NULL;
  304.     hSBarActionProcUPP = NewControlActionProc(hSBarActionProc); /* 04Jan95 e */
  305.     vSBarActionProcUPP = NewControlActionProc(vSBarActionProc); /* 04Jan95 e */
  306. }
  307.  
  308. /* replacement can be installed via eTeSetWordBreak() */
  309.  
  310. static long WordLimits( char *text, long offset, Boolean reverse )
  311. {
  312.     register char *ptr, c;
  313.  
  314.     ptr = text + offset;
  315.     if ( reverse ) {
  316.         /* Scan backwards until we find beginning of word */
  317.         while ( ptr > text ) {
  318.             c = *( ptr - 1 );
  319.             if ( ( c >= 'a' && c <= 'z' ) ||
  320.                  ( c >= 'A' && c <= 'Z' ) || 
  321.                  ( c >= '0' && c <= '9' ) || 
  322.                  ( c == '_' ) )
  323.                 --ptr;
  324.             else
  325.                 break;
  326.         }
  327.     }
  328.     else {
  329.         /* Scan forwards until we find end of word */
  330.         while ( 1 ) {
  331.             c = *ptr;
  332.             if ( ( c >= 'a' && c <= 'z' ) ||
  333.                  ( c >= 'A' && c <= 'Z' ) || 
  334.                  ( c >= '0' && c <= '9' ) || 
  335.                  ( c == '_' ) )
  336.                 ++ptr;
  337.             else
  338.                 break;
  339.         }
  340.     }
  341.     return( ptr - text );
  342. }
  343.  
  344. static short eTeNewRuns( eRec **hE, long len )
  345. {    register long *runPtr;
  346.     register eRec *pE;
  347.     short error;
  348.  
  349.     if ( (**hE).hRuns )
  350.         DisposeHandle( (Handle )(**hE).hRuns );
  351.     (**hE).hRuns = (long **)NewHandle( MIN_RUNS * sizeof( long ) );
  352.       error = LMGetMemErr(); // asm { move.w D0, error } /* error = MemError(); */
  353.       if( error == noErr )
  354.       {    pE = *hE;
  355.         runPtr = *((*pE).hRuns);
  356.         *runPtr++ = 0L;
  357.         *runPtr = len;
  358.         (*pE).runsAllocated = MIN_RUNS;
  359.         (*pE).qRuns = 1;
  360.         (*pE).fustStyle = 0;
  361.     }
  362.     return error;
  363. }
  364.  
  365. long eTeTextLength( eRec **hE )
  366. {
  367.     return (*(**hE).hRuns)[(**hE).qRuns];
  368. }
  369.  
  370. eRec **eTeNew( WindowPtr macPort, Rect viewRect, short tabStops, short wrap,
  371.                  short autoInd, ControlHandle aHSizing, ControlHandle aVSizing )
  372. {    
  373.     register eRec *pE;
  374.     LineRec *linePtr;
  375.     eRec   **hE = (eRec **)NewHandle( sizeof( eRec ) );
  376.  
  377.     if( hE != 0 )
  378.     {    HLock( (Handle )hE );
  379.         pE = *hE;
  380.         (*pE).active = FALSE;
  381.         (*pE).macPort = macPort;
  382.         (*pE).viewRect = viewRect;
  383.         (*pE).width = viewRect.right - viewRect.left;
  384.         (*pE).height = viewRect.bottom - viewRect.top;
  385.         (*pE).hOrigin = -viewRect.left;
  386.         (*pE).vOrigin = -viewRect.top;
  387.         (*pE).position.h = (*pE).position.v = 0;
  388.         (*pE).hScale = (*pE).vScale = 1;
  389.         (*pE).bounds.v = 1;
  390.         (*pE).bounds.h = 1;     /* arbitrary size for now */
  391.         (*pE).leftMargin = 1;    /* room for cursor will blink at the far left */
  392.         (*pE).topMargin = 0;
  393.         (*pE).selStart.h = (*pE).selStart.v = 0;
  394.         (*pE).selEnd = (*pE).selStart;
  395.         (*pE).selActive = FALSE;
  396.         (*pE).caretChPos.h = (*pE).caretChPos.v = 0;
  397.         (*pE).writeChPos.h = (*pE).writeChPos.v = 0;
  398.         (*pE).caretState = CARET_OFF;
  399.         (*pE).maxRight = 0;
  400.         (*pE).eTeWordBreak = (ProcPtr )WordLimits; /* ugh, universal headers kludge 7Jun94 e */
  401.         SetPort( macPort );
  402.         (*pE).tabStops = tabStops;
  403.         (*pE).spaceWidth = 1;
  404.         (*pE).wrap = wrap;                            /*  5Jul92  e  */
  405.         (*pE).autoInd = autoInd;
  406.         (*pE).hText = NewHandle( 1L );
  407.         **((*pE).hText) = '\0';
  408.         (*pE).hLines = (LineRec **)NewHandle( MIN_LINES * sizeof( LineRec ) );
  409.         linePtr = *((*pE).hLines);
  410.         *linePtr++ = 0L;
  411.         *linePtr = 0L;
  412.         (*pE).linesAllocated = MIN_LINES;
  413.         /* 2May92  e  */
  414.         (*pE).hRuns = NULL;
  415.         eTeNewRuns( hE, 0L);
  416.         (*pE).qRuns = 0;
  417.         (*pE).hPrint = NULL;                        /* 28Sep92  e  */
  418.         /* scrollPane */
  419.         (*pE).hStep = (*pE).vStep = 1;
  420.         (*pE).hOverlap = (*pE).vOverlap = 1;
  421.         (*pE).hContext = (*pE).vContext = CONTEXT_LINES;
  422.         if ( ( (*pE).hSBar = aHSizing ) != NULL )
  423.         {    SetControlReference( aHSizing, (long )hE );
  424.             SetControlAction( aHSizing, hSBarActionProcUPP ); /* 04Jan95 e  was: hSBarActionProc */
  425.             /* SetThumbFunc( aHSizing, SBarThumbFunc ); */
  426.         }
  427.         if ( ( (*pE).vSBar = aVSizing ) != NULL )
  428.         {    SetControlReference( aVSizing, (long )hE );
  429.             SetControlAction( aVSizing, vSBarActionProcUPP ); /* 04Jan95 e  was: vSBarActionProc */
  430.             /* SetThumbFunc( aVSizing, SBarThumbFunc ); */
  431.         }
  432.         HUnlock( (Handle )hE );
  433.         eTeSetStyles( hE, &dfltStylNormal, &dfltStylHilite);
  434.         eTeAdjustScrollMax( hE );
  435.         eTeCalibrate( hE );
  436.         (**hE).dirty = FALSE;  /* was (*pE). 14Jul92  e  */
  437.         (**hE).active = TRUE;  /* was (*pE). 14Jul92  e  */
  438.     }
  439.     return hE;
  440. }
  441.  
  442. void eTeDispose( eRec **hE )
  443. {
  444.     if ( (**hE).hText )
  445.         DisposeHandle( (**hE).hText );
  446.     (**hE).hText = NULL;
  447.     if ( (**hE).hLines )
  448.         DisposeHandle( (Handle )(**hE).hLines );
  449.     (**hE).hLines = NULL;
  450.     if ( (**hE).hRuns )
  451.         DisposeHandle( (Handle )(**hE).hRuns );
  452.     (**hE).hRuns = NULL;
  453.     if ( (**hE).hPrint )
  454.         DisposeHandle( (Handle)(**hE).hPrint );    /*  28Sep92  e  */
  455.     DisposeHandle( (Handle )hE );
  456. }
  457.  
  458. static void eTePrepare( eRec **hE )
  459. {
  460.     Rect        tempRect;                /* ClipRect may move memory    */
  461.  
  462.     SetPort( (**hE).macPort );
  463.     tempRect = (**hE).viewRect;
  464.     ClipRect(&tempRect);
  465.     /* obsolete...
  466.     TextFont( (**hE).fontNumber );
  467.     TextSize( (**hE).fontSize );
  468.     TextFace( 0 ); */
  469.     /* Paranoid... */
  470.     TextMode( srcOr );
  471. }
  472.  
  473. static void eTePrepareStyle( eRec **hE, short style )
  474. {
  475.   if( (**hE).curStyle != style )
  476.   { (**hE).curStyle = style;
  477.     TextFont( (**hE).style[style].tsFont );
  478.     TextFace( (**hE).style[style].tsFace );
  479.     TextSize( (**hE).style[style].tsSize );
  480.     /* RGBColor tsColor; */
  481.   }
  482. }
  483.  
  484. static void eTePrepareRun( eRec **hE, short run )
  485. {
  486.     eTePrepareStyle( hE, eTeRunToStyle( hE, run ) );
  487. }
  488.  
  489. static void eTeRefresh( eRec **hE )
  490. {
  491.     SetPort( (**hE).macPort );
  492.     InvalRect( &(**hE).viewRect );
  493. }
  494.  
  495. static void eTeUpdateCaretRect( eRec **hE )
  496. {
  497.     Point    tempPt;
  498.     register eRec    *pE;
  499.  
  500.     tempPt = eTeChPosToPoint( hE, (**hE).caretChPos );
  501.     pE = *hE;
  502.     (*pE).caretRect.top    = tempPt.v;
  503.     (*pE).caretRect.right  = tempPt.h;
  504.     (*pE).caretRect.left   = tempPt.h - 1;
  505.     (*pE).caretRect.bottom = tempPt.v + (*pE).caretHeight;
  506. }
  507.  
  508. void eTeSetWrap( eRec **hE, short wrap )
  509. {
  510.     /* 22Jul92  e  -- added limits */
  511.     if( wrap < 1 ) wrap = 1;
  512.     else if( wrap > 999 ) wrap = 999;
  513.     /* (**hE).wrap = wrap; */
  514.     /* 10Aug92  e   need to fix all ChPos for new wrap! */
  515.     if( (**hE).wrap != wrap )
  516.     { long car = eTeChPosToOffset( hE, (**hE).caretChPos );
  517.       long sta = eTeChPosToOffset( hE, (**hE).selStart );
  518.       long end = eTeChPosToOffset( hE, (**hE).selEnd );
  519.       long wri = eTeChPosToOffset( hE, (**hE).writeChPos );
  520.       (**hE).wrap = wrap;
  521.       eTeUpdateLineStarts( hE, 0 );
  522.       (**hE).caretChPos = eTeOffsetToChPos( hE, car );
  523.       (**hE).selStart   = eTeOffsetToChPos( hE, sta );
  524.       (**hE).selEnd     = eTeOffsetToChPos( hE, end );
  525.       (**hE).writeChPos = eTeOffsetToChPos( hE, wri );
  526.       eTeUpdateCaretRect( hE );
  527.       eTeRefresh( hE );
  528.     }
  529. }
  530.  
  531. void eTeSetTabStop( eRec **hE, short aTabStop )
  532. {
  533.     /* Undo? */
  534.     (**hE).tabStops = aTabStop;
  535.     (**hE).tabWidth = aTabStop * (**hE).spaceWidth;
  536.     eTeRefresh( hE );
  537. }
  538.  
  539. static short eTeTabStop( eRec **hE, register short curPosition )
  540. {
  541.     register short tabWidth = (**hE).tabWidth;
  542.     return (curPosition +
  543.              (tabWidth -
  544.                ((curPosition - (**hE).leftMargin + (**hE).hOrigin) % tabWidth)));
  545. }
  546.  
  547. void eTeSetWordBreak( eRec **hE, ProcPtr aFunc )
  548. {
  549.     if ( aFunc == NULL )
  550.         (**hE).eTeWordBreak = (ProcPtr )WordLimits; /* ugh, universal headers kludge 7Jun94 e */
  551.     else
  552.         (**hE).eTeWordBreak = aFunc;
  553. }
  554.  
  555. /* 23Jul92  e  */
  556.  
  557. /* Inverts the text between the start and stop positions. */
  558.  
  559. static void eTeHiliteRange( eRec **hE, register ChPos start, register ChPos stop )
  560. {
  561.     Rect tmpRect;
  562.     Point stopPt;
  563.     register short vScale;
  564.     
  565.     if ( ! (**hE).active )
  566.         return;
  567.  
  568.     topLeft( tmpRect ) = eTeChPosToPoint( hE, start );
  569.     tmpRect.left -= 1;
  570.  
  571.     vScale = (**hE).vScale;
  572.  
  573.     /* Just need to hilite within same line */
  574.     if ( start.v == stop.v ) {
  575.         botRight( tmpRect ) = eTeChPosToPoint( hE, stop );
  576.         tmpRect.right -= 1;
  577.         tmpRect.bottom = tmpRect.top + vScale;
  578.         if ( tmpRect.left != tmpRect.right ) {
  579.             eTePrepare( hE );
  580. #ifndef __CONDITIONALMACROS__
  581.             asm { bclr #hiliteBit, HiliteMode }
  582. #else
  583.             LMSetHiliteMode(LMGetHiliteMode() & 0x7F);
  584. #endif
  585.             InvertRect( &tmpRect );
  586.         }
  587.     }
  588.     /* Hilite spans more than one line */
  589.     else
  590.     {    stopPt = eTeChPosToPoint( hE, stop );
  591.         if( tmpRect.top < (**hE).viewRect.bottom && stopPt.v >= (**hE).viewRect.top )
  592.         {    eTePrepare( hE );
  593.             tmpRect.right = (**hE).viewRect.right;
  594.             tmpRect.bottom = tmpRect.top + vScale;
  595.             if( tmpRect.bottom > (**hE).viewRect.top )
  596.             {
  597. #ifndef __CONDITIONALMACROS__
  598.                 asm { bclr #hiliteBit, HiliteMode }
  599. #else
  600.                 LMSetHiliteMode(LMGetHiliteMode() & 0x7F);
  601. #endif
  602.                 InvertRect( &tmpRect );
  603.                 tmpRect.top += vScale;
  604.             }
  605.             else
  606.                 tmpRect.top = (**hE).viewRect.top;
  607.             tmpRect.left = (**hE).viewRect.left;
  608.             tmpRect.bottom = Min( stopPt.v, (**hE).viewRect.bottom );
  609. #ifndef __CONDITIONALMACROS__
  610.             asm { bclr #hiliteBit, HiliteMode }
  611. #else
  612.             LMSetHiliteMode(LMGetHiliteMode() & 0x7F);
  613. #endif
  614.             InvertRect( &tmpRect );
  615.             if( stopPt.v < (**hE).viewRect.bottom )
  616.             {    tmpRect.bottom += vScale;
  617.                 tmpRect.top = stopPt.v;
  618.                 tmpRect.right = stopPt.h - 1;
  619. #ifndef __CONDITIONALMACROS__
  620.                 asm { bclr #hiliteBit, HiliteMode }
  621. #else
  622.                 LMSetHiliteMode(LMGetHiliteMode() & 0x7F); /* ~hiliteBit 4 times 18Jul94 e */
  623. #endif
  624.                 InvertRect( &tmpRect );
  625.             }
  626.         }
  627.     }
  628. }
  629.  
  630. /* 23Jul92  e  */
  631.  
  632. static void eTeSetCaretState( eRec **hE, Boolean state )
  633. {
  634.     Rect    tmpRect;
  635.     register eRec    *pE = *hE;
  636.  
  637.     if (   ! (*pE).selActive
  638.           && (*pE).active
  639.           && (*pE).caretState != state )
  640.     {
  641.         (*pE).caretState = state;
  642.         tmpRect = (*pE).caretRect;
  643.         eTePrepare( hE );
  644.         PenMode(patXor);
  645.         /* FrameRect( &tmpRect ); */
  646.         MoveTo( tmpRect.right, tmpRect.top );
  647.         LineTo( tmpRect.right, tmpRect.bottom );
  648.         /* 13Apr 93  e */
  649.         PenNormal();
  650.     }
  651. }
  652.  
  653. void eTeActivate( eRec **hE )
  654. {
  655.     if( (**hE).active == FALSE )
  656.     {    (**hE).active = TRUE;
  657.         if( (**hE).selActive )
  658.             eTeHiliteRange( hE, (**hE).selStart, (**hE).selEnd );
  659.     }
  660. }
  661.  
  662. void eTeDeactivate( eRec **hE )
  663. {
  664.     if( (**hE).active == TRUE )
  665.     {    if( (**hE).selActive )
  666.             eTeHiliteRange( hE, (**hE).selStart, (**hE).selEnd );
  667.         else
  668.             eTeSetCaretState( hE, CARET_OFF );
  669.         (**hE).active = FALSE;
  670.     }
  671. }
  672.  
  673. void eTeIdle( eRec **hE )
  674. {
  675.     static long lastCaretToggle = 0L;
  676.     register long    now;
  677.  
  678.     gMaxSleep = GetCaretTime();    /* user may change it using desk accessory */
  679.     now = TickCount();
  680.     if ( now - lastCaretToggle >= gMaxSleep ) {
  681.         lastCaretToggle = now;
  682.         eTeSetCaretState( hE, (**hE).caretState == CARET_OFF ? CARET_ON : CARET_OFF );
  683.     }
  684.     else {
  685.         gMaxSleep -= ( now - lastCaretToggle );
  686.     }
  687. }
  688.  
  689. static void eTeResize( eRec **hE, register Rect *delta )
  690. {
  691.     register eRec *pE = *hE;
  692.  
  693.     (*pE).width      += delta->right - delta->left;
  694.     (*pE).height     += delta->bottom - delta->top;
  695.     (*pE).leftMargin += delta->left;
  696.     (*pE).topMargin  += delta->top;
  697.  
  698.     (*pE).hOrigin -= delta->left;
  699.     (*pE).vOrigin -= delta->top;
  700.  
  701.     eTeUpdateCaretRect( hE );
  702.     eTeAdjustScrollMax( hE );
  703.     eTeCalibrate( hE );
  704. }
  705.  
  706. void eTeNewView( eRec **hE, Rect *viewRect )
  707. {
  708.     register eRec *pE = *hE;
  709.     Rect    delta;
  710.     
  711.     delta = *viewRect;
  712.     delta.top -= (*pE).viewRect.top;
  713.     delta.left -= (*pE).viewRect.left;
  714.     delta.bottom -= (*pE).viewRect.bottom;
  715.     delta.right -= (*pE).viewRect.right;
  716.     (*pE).viewRect   = *viewRect;
  717.     
  718.     eTeResize( hE, &delta );
  719. }
  720.  
  721. /* eTeSetStyles also causes the display to be redrawn */
  722.  
  723. void eTeSetStyles( eRec **hE, TextStyle *ts0, TextStyle *ts1 )
  724. {
  725.     short        height[2];
  726.     FontInfo    fontInfo[2];
  727.     ChPos        tmpPt;
  728.     short        bigger;
  729.  
  730.     (**hE).style[0] = *ts0;
  731.     (**hE).style[1] = *ts1;
  732.     (**hE).curStyle = NOSTYLE;
  733.  
  734.     /* Turn off cursor */
  735.     eTePrepare( hE );
  736.     eTeSetCaretState( hE, CARET_OFF );
  737.  
  738.     eTePrepareStyle( hE, 1 );
  739.     GetFontInfo( &fontInfo[1] );
  740.     height[1] = CharWidth( OPTION_SPACE );
  741.  
  742.     eTePrepareStyle( hE, 0 );
  743.     GetFontInfo( &fontInfo[0] );
  744.     height[0] = CharWidth( OPTION_SPACE );
  745.     
  746.     (**hE).spaceWidth = Max ( height[0], height[1] );
  747.     /* calculate new tabstop based on new font/size */
  748.     (**hE).tabWidth = (**hE).tabStops * (**hE).spaceWidth;
  749.  
  750.     height[0] = fontInfo[0].ascent + fontInfo[0].descent;
  751.     height[1] = fontInfo[1].ascent + fontInfo[1].descent;
  752.     
  753.     bigger = height[1] > height[0] ? 1 : 0;
  754.     (**hE).fontAscent = fontInfo[bigger].ascent;
  755.     (**hE).caretHeight = height[bigger];
  756.  
  757.     /* Update caret values for new font */
  758.     eTeUpdateCaretRect( hE );
  759.     (**hE).maxRight = (**hE).caretRect.right;
  760.  
  761.     /*
  762.      * Set the scale values for our scrollbars. Vertical scrolling is done by 
  763.      * lines; horizontal by widest character. First, move to top of panorama (but
  764.      * don't redraw), then change the scale values. Finally, move back to original
  765.      * position and redraw screen.
  766.      */
  767.     tmpPt = (**hE).position;
  768.     eTeScroll( hE, -(**hE).position.h, -(**hE).position.v, FALSE );
  769.     (**hE).hScale = Max( fontInfo[0].widMax, fontInfo[1].widMax );
  770.     (**hE).vScale = (**hE).caretHeight + fontInfo[bigger].leading;
  771.     eTeAdjustScrollMax( hE );
  772.     eTeScrollTo( hE, tmpPt, FALSE );
  773.     eTeRefresh( hE );
  774. }
  775.  
  776. #ifdef include_obsolete_code
  777.  
  778. void eTeSetFontNumber( eRec **hE, short aFontNumber )
  779. {
  780.     Str255        fontName;
  781.  
  782.     GetFontName( aFontNumber, fontName );
  783.     if ( fontName[0] == 0 )
  784.         aFontNumber = 0;    /* If font does not exist, then use the system font */
  785.  
  786.     /* Undo? */
  787.     (**hE).fontNumber = aFontNumber;
  788.     eTeFontChanged( hE );
  789. }
  790.  
  791. void eTeSetFontName( eRec **hE, Str255 aFontName )
  792. {
  793.     short    fontNumber;
  794.  
  795.     GetFNum( aFontName, &fontNumber );
  796.     eTeSetFontNumber( hE, ( fontNumber > 0 ) ? fontNumber : 0 );
  797. }
  798.  
  799. void eTeSetFontSize( eRec **hE, short aFontSize )
  800. {
  801.     /* Undo? */
  802.     (**hE).fontSize = aFontSize;
  803.     eTeFontChanged( hE );
  804. }
  805.  
  806. #endif
  807.  
  808. /* static --  25Sep92  e  for os_mac_Print.c */
  809. void eTeDrawLine( eRec **hE, ChPos beginPos, Point location )
  810. {
  811.     register char    c, *charPtr, *firstChar;
  812.     register short    count;
  813.     long     offset, instyle, inlyne, eol;
  814.     short     run;
  815.     Point    pt;
  816.  
  817.     HLock( (**hE).hText );
  818.     MoveTo( location.h, location.v + (**hE).fontAscent );
  819.     
  820.     offset = *(*(**hE).hLines + beginPos.v) + beginPos.h;
  821.     charPtr = *(**hE).hText + offset;
  822.     eol    = *(*(**hE).hLines + beginPos.v + 1);
  823.     inlyne = eol - offset;
  824.     run = eTeOffsetToRun( hE, offset );
  825.     while( inlyne )
  826.     {    eTePrepareRun( hE, run );
  827.         instyle = (*(**hE).hRuns)[++run] - offset;
  828.         if( instyle > inlyne )
  829.             instyle = inlyne;
  830.         inlyne -= instyle;
  831.         offset += instyle;
  832.         firstChar = charPtr;
  833.         count = 0;
  834.         while ( instyle && ( c = *charPtr++ ) != RETURN )    /* 11Aug92  e   was: instyle-- */
  835.         {    if ( c == TAB ) 
  836.             {    if ( count > 0 ) DrawText( firstChar, 0, count );
  837.                 GetPen( &pt );
  838.                 pt.h = eTeTabStop( hE, pt.h );
  839.                 MoveTo( pt.h, pt.v );
  840.                 firstChar = charPtr;
  841.                 count = 0;
  842.             }
  843.             else ++count;
  844.             instyle -= 1;                                    /* 11Aug92  e  */
  845.         }
  846.         if ( count  > 0 ) DrawText( firstChar, 0, count );
  847.     }
  848.     if( c != RETURN && *charPtr != '\0' ) DrawChar( 0xd7 );
  849.     /* if( instyle == 0 && *charPtr != '\0' ) DrawChar( 0xd7 ); /* NG!? 11Aug92  e  */
  850.     HUnlock( (**hE).hText );
  851. }
  852.  
  853. void eTeDraw( eRec **hE, Rect *area )
  854. {
  855.     eRec *pE;
  856.     short    vFirst, vLast;
  857.     ChPos    startPos;
  858.     Point    location;
  859.     Rect    tRect;
  860.     Boolean doCaret;    /* 23Jul92  e */
  861.     
  862.     HLock( (Handle )hE );
  863.     pE = *hE;
  864.     
  865.     if ( SectRect( area, &(*pE).viewRect, &tRect ) )
  866.     {
  867.         eTePrepare( hE );
  868.         
  869.         doCaret = ( ( ! (*pE).selActive ) && ( (*pE).caretState == CARET_ON ) );
  870.         if( doCaret ) eTeSetCaretState( hE, CARET_OFF );
  871.  
  872.         vFirst = ( tRect.top - (*pE).topMargin + (*pE).vOrigin ) / (*pE).vScale;
  873.         vLast = ( tRect.bottom + (*pE).vScale - (*pE).topMargin + (*pE).vOrigin - 1 )
  874.                 / (*pE).vScale;
  875.  
  876.         if ( vFirst < 0 )
  877.             vFirst = 0;
  878.         if ( vLast >= (*pE).bounds.v ) 
  879.             vLast = (*pE).bounds.v - 1;
  880.  
  881.         EraseRect( &tRect );
  882.  
  883.         if ( vFirst < (*pE).bounds.v && vLast >= 0)
  884.  
  885.         {    location.h = (*pE).leftMargin - (*pE).hOrigin;
  886.             location.v = vFirst * (*pE).vScale + (*pE).topMargin - (*pE).vOrigin;
  887.             startPos.h = 0;
  888.  
  889.             for ( startPos.v = vFirst;
  890.                   startPos.v <= vLast;
  891.                   location.v += (*pE).vScale, ++startPos.v )
  892.             {    eTeDrawLine( hE, startPos, location );
  893.             }
  894.             if ( (*pE).selActive )
  895.                  eTeHiliteRange( hE, (*pE).selStart, (*pE).selEnd );    /* may have munged it */
  896.             else if( doCaret )
  897.                 eTeSetCaretState( hE, CARET_ON );
  898.         }
  899.     }
  900.     HUnlock( (Handle )hE );
  901. }
  902.  
  903. void eTeUpdate( eRec **hE )
  904. {
  905.     Rect tempRect;
  906.     
  907.     eTePrepare( hE );
  908.     tempRect = (**((**hE).macPort)->visRgn).rgnBBox;
  909.     eTeDraw( hE, &tempRect );
  910. }
  911.         
  912. #ifndef THINK_ARROWS
  913.  
  914. static void eTeExtSelGuts( eRec **hE, ChPos chPos )
  915. {
  916.     if( PTL( chPos ) < PTL( (**hE).caretChPos ) )
  917.     {    eTeHiliteRange( hE, chPos, (**hE).caretChPos );
  918.         if( PTL( (**hE).caretChPos ) == PTL( (**hE).selStart ) )
  919.             (**hE).selStart = chPos;
  920.         else if( PTL( chPos ) >= PTL( (**hE).selStart ) )
  921.             (**hE).selEnd = chPos;
  922.         else
  923.         {    (**hE).selEnd = (**hE).selStart;
  924.             (**hE).selStart = chPos;
  925.         }
  926.     }
  927.     else if( PTL( chPos ) > PTL( (**hE).caretChPos ) )
  928.     {    eTeHiliteRange( hE, (**hE).caretChPos, chPos );
  929.         if( PTL( (**hE).caretChPos ) == PTL( (**hE).selEnd ) )
  930.             (**hE).selEnd = chPos;
  931.         else if( PTL( chPos ) <= PTL( (**hE).selEnd ) )
  932.             (**hE).selStart = chPos;
  933.         else
  934.         {    (**hE).selStart = (**hE).selEnd;
  935.             (**hE).selEnd = chPos;
  936.         }
  937.     }
  938. }
  939.  
  940. #endif
  941.  
  942. static void eTeEnsureChPos( eRec **hE, ChPos chPos )
  943. {
  944.     if( PTL( chPos ) != PTL( (**hE).caretChPos ) )
  945.     {    (**hE).caretChPos = chPos;
  946.         eTeUpdateCaretRect( hE );
  947.         (**hE).maxRight = (**hE).caretRect.right;
  948.     }
  949. }
  950.  
  951. void eTeKey( eRec **hE, char theChar, Byte keyCode, short modifiers, short style )
  952. {
  953.     LineRec        *linePtr;
  954.     long        offset;
  955.     ChPos        chPos;
  956.     char        chars[ 255 ];
  957.     Boolean        shifted, optioned, commanded;
  958.     long        new;
  959.     Point         aPt;
  960.  
  961.     ObscureCursor();
  962.     eTeSetCaretState( hE, CARET_OFF ); /* move up here 22Jul92  e  */
  963.  
  964.     shifted = ( modifiers & shiftKey ) ? 1 : 0;
  965.     optioned = ( modifiers & optionKey ) ? 1 : 0;
  966.     commanded = ( modifiers & cmdKey ) ? 1 : 0;
  967.  
  968.     switch ( keyCode ) {
  969.         case KeyF1:
  970.             /* Undo */
  971.             break;
  972.         case KeyF2:
  973.             eTeCut( hE );
  974.             break;
  975.         case KeyF3:
  976.             eTeCopy( hE );
  977.             break;
  978.         case KeyF4:
  979.             /* eTePaste( hE, style );            /* correct        3May92  e */
  980.             eTePaste( hE, style ^ optioned );    /* for debugging  3May92  e */
  981.             break;
  982.         case KeyF5:
  983.         case KeyF6:
  984.         case KeyF7:
  985.         case KeyF8:
  986.         case KeyF9:
  987.         case KeyF10:
  988.         case KeyF11:
  989.         case KeyF12:
  990.         case KeyF13:
  991.         case KeyF14:
  992.         case KeyF15:
  993.             /* DoFunctionKey( theChar, keyCode, modifiers ); */
  994.             break;
  995.  
  996.         case KeyHome:                                            /*** HOME ***/
  997.             eTeScrollTo( hE, zeroPos, TRUE );
  998.             break;
  999.  
  1000.         case KeyEnd:                                            /*** END ***/
  1001.             chPos.v = (**hE).bounds.v - (**hE).vSpan;
  1002.             if ( chPos.v < 0 )
  1003.                 chPos.v = 0;
  1004.             chPos.h = (**hE).position.h;
  1005.             eTeScrollTo( hE, chPos, TRUE );
  1006.             break;
  1007.  
  1008.         case KeyPageUp:                                            /*** PAGE UP ***/
  1009.             eTeDoVscroll( hE, kControlPageUpPart ); // inPageUp
  1010.             eTeAdjustScrollMax( hE );
  1011.             break;
  1012.  
  1013.         case KeyPageDown:                                        /*** PAGE DOWN ***/
  1014.             eTeDoVscroll( hE, kControlPageDownPart ); // inPageDown
  1015.             eTeAdjustScrollMax( hE );
  1016.             break;
  1017.  
  1018.         case KeyDel:                                            /*** DEL FWD ***/
  1019.             offset = eTeChPosToOffset( hE, (**hE).caretChPos );
  1020.             if ( (**hE).selActive )
  1021.                 eTeDelete( hE );
  1022.             else if ( optioned ) { /* delete to end of line */
  1023.                 /* 5Jul92  e */
  1024.                 chPos = (**hE).caretChPos;
  1025.                 linePtr = *(**hE).hLines;
  1026.               labelTryAgainK:
  1027.                 chPos.h = linePtr[ chPos.v + 1 ] - linePtr[ chPos.v ];
  1028.                 if ( chPos.v < (**hE).bounds.v - 1 )
  1029.                 { --chPos.h;
  1030.                   new = eTeChPosToOffset( hE, chPos );
  1031.                   if( *(*(**hE).hText + new) != RETURN )
  1032.                   { chPos.v++;
  1033.                     goto labelTryAgainK;
  1034.                   }
  1035.                   if( new == offset )
  1036.                       chPos.h++;    /* delete the RETURN if its all there is */
  1037.                 }
  1038.                 eTeKillTo( hE, chPos );
  1039.             }
  1040.             else if ( *(*(**hE).hText + offset) != '\0' ) {
  1041.                 /* 14Aug92  e   for undo...
  1042.                 eTeEdGuts( hE, EMPTY_PTR, 0L, *(*(**hE).hText + offset) == RETURN, TRUE, style );
  1043.                 */
  1044.                 eTeKillTo( hE, eTeOffsetToChPos( hE, offset + 1 ) ); /* optimize for Undo Typing */
  1045.             }
  1046.             break;
  1047.  
  1048.         default:
  1049.             MoveHHi( (Handle )(**hE).hLines );
  1050.             HLock( (Handle )(**hE).hLines );
  1051.             /* eTeSetCaretState( hE, CARET_OFF );  --  move above  22Jul92  e  */
  1052.             linePtr = *(**hE).hLines;
  1053.             switch( theChar ) {
  1054.                 case LEFT_ARROW:                                /*** LEFT ARROW ***/
  1055.                     if ( (**hE).selActive && ! shifted )
  1056.                     {    /* if no shift key and there is a selection, remove it */
  1057.                         eTeHiliteRange( hE, (**hE).selStart, (**hE).selEnd );
  1058.                         chPos = (**hE).selEnd = (**hE).selStart;
  1059.                     }
  1060. #ifndef THINK_ARROWS
  1061.                     else if( (**hE).selActive && optioned && commanded )
  1062.                     {    /* move chPos to start of selection if not there */
  1063.                         chPos = (**hE).selStart;
  1064.                         eTeEnsureChPos( hE, chPos );
  1065.                     }
  1066. #endif
  1067.                     else
  1068.                     {    if ( shifted )
  1069.                           {    if ( ! (**hE).selActive )
  1070.                                 (**hE).selStart = (**hE).selEnd = (**hE).caretChPos;
  1071. #ifdef THINK_ARROWS
  1072.                             (**hE).caretChPos = (**hE).selStart;
  1073. #endif
  1074.                         }
  1075.                         chPos = (**hE).caretChPos;
  1076.                         if ( commanded )
  1077.                         {    chPos.h = 0;    /* move to beginning of line */
  1078.                             labelTryAgainL:    /* added the rest 5Jul92  e  */
  1079.                             if ( chPos.v > 0 )
  1080.                             {    offset = eTeChPosToOffset( hE, chPos );
  1081.                                 if( *(*(**hE).hText + offset - 1) != RETURN )
  1082.                                 {    chPos.v--;
  1083.                                     goto labelTryAgainL;
  1084.                                 }
  1085.                             }
  1086.                         }
  1087.                         else if ( optioned )
  1088.                         {                     /* move to start/end of previous word */
  1089.                               offset = eTeChPosToOffset( hE, chPos );
  1090.                             new = WordLimits( *(**hE).hText, offset, TRUE );
  1091.                             if ( new == offset && offset != 0L )
  1092.                                 --new;
  1093.                             chPos = eTeOffsetToChPos( hE, new );
  1094.                         }
  1095.                           else                 /* move to previous character position */
  1096.                             chPos = eTeOffsetToChPos( hE, eTeChPosToOffset( hE, chPos ) - 1 );
  1097.                         if ( shifted )
  1098.                         {
  1099. #ifdef THINK_ARROWS
  1100.                             eTeHiliteRange( hE, chPos, (**hE).selStart );
  1101.                             (**hE).selStart = chPos;
  1102. #else
  1103.                             eTeExtSelGuts( hE, chPos );
  1104. #endif
  1105.                             /* 30Apr92  e  -- needed if newly active AND scrolling... */
  1106.                             (**hE).selActive = ( PTL( (**hE).selStart ) != PTL( (**hE).selEnd ) );
  1107.                         }
  1108.                     }
  1109.                     (**hE).caretChPos = chPos;
  1110.                     eTeUpdateCaretRect( hE );
  1111.                     (**hE).maxRight = (**hE).caretRect.right;
  1112.                     eTeShowCaret( hE );
  1113.                     break;
  1114.  
  1115.                 case RIGHT_ARROW:                                /*** RIGHT ARROW ***/
  1116.                     if ( (**hE).selActive && ! shifted )
  1117.                     {    /* if no shift key and there is a selection, remove it */
  1118.                         eTeHiliteRange( hE, (**hE).selStart, (**hE).selEnd );
  1119.                         chPos = (**hE).selStart = (**hE).selEnd;
  1120.                     }
  1121. #ifndef THINK_ARROWS
  1122.                     else if( (**hE).selActive && optioned && commanded )
  1123.                     {    /* move chPos to end of selection if not there */
  1124.                         chPos = (**hE).selEnd;
  1125.                         eTeEnsureChPos( hE, chPos );
  1126.                     }
  1127. #endif
  1128.                     else
  1129.                     {    if ( shifted )
  1130.                           {    if ( ! (**hE).selActive )
  1131.                                 (**hE).selStart = (**hE).selEnd = (**hE).caretChPos;
  1132. #ifdef THINK_ARROWS
  1133.                             (**hE).caretChPos = (**hE).selEnd;
  1134. #endif
  1135.                         }
  1136.                         chPos = (**hE).caretChPos;
  1137.                         if ( commanded )
  1138.                         {                    /* move to end of line */
  1139.                             labelTryAgainR:
  1140.                             chPos.h = linePtr[ chPos.v + 1 ] - linePtr[ chPos.v ];
  1141.                             if ( chPos.v < (**hE).bounds.v - 1 )
  1142.                             {    /* 5Jul92  e  was: --chPos.h; */
  1143.                                 --chPos.h;
  1144.                                 offset = eTeChPosToOffset( hE, chPos );
  1145.                                 if( *(*(**hE).hText + offset) != RETURN )
  1146.                                 {    chPos.v++;
  1147.                                     goto labelTryAgainR;
  1148.                                 }
  1149.                             }
  1150.                         }
  1151.                         else if ( optioned )
  1152.                         {                     /* move to start/end of next word */
  1153.                               offset = eTeChPosToOffset( hE, chPos );
  1154.                             new = WordLimits( *(**hE).hText, offset, FALSE );
  1155.                             if ( new == offset && *(*(**hE).hText + offset) != '\0' )
  1156.                                 ++new;
  1157.                             chPos = eTeOffsetToChPos( hE, new );
  1158.                         }
  1159.                           else                 /* move to previous character position */
  1160.                             chPos = eTeOffsetToChPos( hE, eTeChPosToOffset( hE, chPos ) + 1 );
  1161.                         if ( shifted )
  1162.                         {
  1163. #ifdef THINK_ARROWS
  1164.                             eTeHiliteRange( hE, (**hE).selEnd, chPos );
  1165.                             (**hE).selEnd = chPos;
  1166. #else
  1167.                             eTeExtSelGuts( hE, chPos );
  1168. #endif
  1169.                             /* 30Apr92  e  -- needed if newly active AND scrolling... */
  1170.                             (**hE).selActive = ( PTL( (**hE).selStart ) != PTL( (**hE).selEnd ) );
  1171.                         }
  1172.                     }
  1173.                     (**hE).caretChPos = chPos;
  1174.                     eTeUpdateCaretRect( hE );
  1175.                     (**hE).maxRight = (**hE).caretRect.right;
  1176.                     eTeShowCaret( hE );
  1177.                     break;
  1178.  
  1179.                 case UP_ARROW:                                /*** UP ARROW ***/
  1180.                     if ( (**hE).selActive && ! shifted )
  1181.                     {    /* if no shift key and there is a selection, remove it */
  1182.                         eTeHiliteRange( hE, (**hE).selStart, (**hE).selEnd );
  1183.                         chPos = (**hE).selEnd = (**hE).selStart;
  1184.                     }
  1185. #ifndef THINK_ARROWS
  1186.                     else if( (**hE).selActive && optioned && commanded )
  1187.                     {    /* move chPos to start of selection if not there */
  1188.                         chPos = (**hE).selStart;
  1189.                         eTeEnsureChPos( hE, chPos );
  1190.                     }
  1191. #endif
  1192.                     else
  1193.                     {    if ( shifted )
  1194.                           {    if ( ! (**hE).selActive )
  1195.                                 (**hE).selStart = (**hE).selEnd = (**hE).caretChPos;
  1196. #ifdef THINK_ARROWS
  1197.                             else
  1198.                                 eTeEnsureChPos( hE, (**hE).selStart );
  1199. #endif
  1200.                         }
  1201.                         chPos = (**hE).caretChPos;
  1202.                         if ( commanded )
  1203.                             chPos.h = chPos.v = 0;    /* move to beginning of text */
  1204.                         else if( optioned )
  1205.                         {    aPt.h = (**hE).maxRight;
  1206.                             chPos.v -= (**hE).vSpan - (**hE).vOverlap;    /* move up one page */
  1207.                             if( chPos.v < 0 ) chPos.v = 0;
  1208.                             chPos = eTePointHtoChPosH( hE, aPt, chPos );
  1209.                         }
  1210.                           else if ( chPos.v > 0 )
  1211.                         {    /* NG...   [caretRect.top can be bogus!]
  1212.                             aPt.h = (**hE).maxRight;
  1213.                             aPt.v = (**hE).caretRect.top - (**hE).vScale;
  1214.                             chPos = eTePointToChPos( hE, aPt );
  1215.                             */
  1216.                             aPt.h = (**hE).maxRight;
  1217.                             chPos.v -= 1;    /* move up one line */
  1218.                             chPos = eTePointHtoChPosH( hE, aPt, chPos );
  1219.                         }
  1220.                         if ( shifted )
  1221.                         {
  1222. #ifdef THINK_ARROWS
  1223.                             eTeHiliteRange( hE, chPos, (**hE).selStart );
  1224.                             (**hE).selStart = chPos;
  1225. #else
  1226.                             eTeExtSelGuts( hE, chPos );
  1227. #endif
  1228.                             /* 30Apr92  e  -- needed if newly active AND scrolling... */
  1229.                             (**hE).selActive = ( PTL( (**hE).selStart ) != PTL( (**hE).selEnd ) );
  1230.                         }
  1231.                     }
  1232.                     (**hE).caretChPos = chPos;
  1233.                     eTeUpdateCaretRect( hE );
  1234.                     eTeShowCaret( hE );
  1235.                     break;
  1236.  
  1237.                 case DOWN_ARROW:                            /*** DOWN ARROW ***/
  1238.                     if ( (**hE).selActive && ! shifted )
  1239.                     {    /* if no shift key and there is a selection, remove it */
  1240.                         eTeHiliteRange( hE, (**hE).selStart, (**hE).selEnd );
  1241.                         chPos = (**hE).selStart = (**hE).selEnd;
  1242.                     }
  1243. #ifndef THINK_ARROWS
  1244.                     else if( (**hE).selActive && optioned && commanded )
  1245.                     {    /* move chPos to end of selection if not there */
  1246.                         chPos = (**hE).selEnd;
  1247.                         eTeEnsureChPos( hE, chPos );
  1248.                     }
  1249. #endif
  1250.                     else
  1251.                     {    if ( shifted )
  1252.                           {    if ( ! (**hE).selActive )
  1253.                                 (**hE).selStart = (**hE).selEnd = (**hE).caretChPos;
  1254. #ifdef THINK_ARROWS
  1255.                             else
  1256.                                 eTeEnsureChPos( hE, (**hE).selEnd );
  1257. #endif
  1258.                         }
  1259.                         chPos = (**hE).caretChPos;
  1260.                         if ( commanded )
  1261.                         {                    /* move to end of text */
  1262.                             chPos.v = (**hE).bounds.v - 1;
  1263.                             chPos.h = linePtr[ chPos.v + 1 ] - linePtr[ chPos.v ];
  1264.                         }
  1265.                         else if( optioned )
  1266.                         {    aPt.h = (**hE).maxRight;
  1267.                             if( chPos.v > (**hE).bounds.v - (**hE).vSpan + (**hE).vOverlap )
  1268.                                 chPos.v = (**hE).bounds.v - 1;
  1269.                             else
  1270.                                 chPos.v += (**hE).vSpan - (**hE).vOverlap;    /* move down one page */
  1271.                             chPos = eTePointHtoChPosH( hE, aPt, chPos );
  1272.                         }
  1273.                           else if ( chPos.v < (**hE).bounds.v - 1 )
  1274.                         {    /* NG...   [caretRect.top can be bogus!]
  1275.                             aPt.h = (**hE).maxRight;
  1276.                             aPt.v = (**hE).caretRect.top + (**hE).vScale;
  1277.                             chPos = eTePointToChPos( hE, aPt );
  1278.                             */
  1279.                             aPt.h = (**hE).maxRight;
  1280.                             chPos.v += 1;    /* move down one line */
  1281.                             chPos = eTePointHtoChPosH( hE, aPt, chPos );
  1282.                         }
  1283.                         if ( shifted )
  1284.                         {
  1285. #ifdef THINK_ARROWS
  1286.                             eTeHiliteRange( hE, (**hE).selEnd, chPos );
  1287.                             (**hE).selEnd = chPos;
  1288. #else
  1289.                             eTeExtSelGuts( hE, chPos );
  1290. #endif
  1291.                             /* 30Apr92  e  -- needed if newly active AND scrolling... */
  1292.                             (**hE).selActive = ( PTL( (**hE).selStart ) != PTL( (**hE).selEnd ) );
  1293.                         }
  1294.                     }
  1295.                     (**hE).caretChPos = chPos;
  1296.                     eTeUpdateCaretRect( hE );
  1297.                     eTeShowCaret( hE );
  1298.                     break;
  1299.  
  1300.                 case DELETE:                                /*** DELETE ***/
  1301.                     eTeDelete( hE );
  1302.                     break;
  1303.  
  1304.                 default:                                    /*** TEXT ***/
  1305.                     /* Undo? */
  1306.                     if ( theChar == ENTER || theChar == RETURN ) {
  1307.                         chars[ 0 ] = RETURN;
  1308.                         offset = 1;
  1309. #ifdef THINK_RETURN
  1310.                         if ( (**hE).caretChPos.v > 0 ) {
  1311.                             charPtr = *(**hE).hText + linePtr[ (**hE).caretChPos.v - 1 ];
  1312.                             while ( *charPtr == TAB || *charPtr == SPACE ) {
  1313.                                 chars[ offset++ ] = *charPtr++;
  1314.                             }
  1315.                         }
  1316. #endif
  1317.                     }
  1318. #ifdef CHAR8_OK
  1319.                     else if ( theChar == TAB || (unsigned char)theChar >= SPACE )
  1320. #else
  1321.                     else if ( theChar == TAB ||                theChar >= SPACE )
  1322. #endif
  1323.                     {
  1324.                         chars[0] = theChar;
  1325.                         offset = 1;
  1326.                     }
  1327.                     else {
  1328.                         break;
  1329.                     }
  1330.                     /* Insert character to right of cursor position */
  1331.                     /*  14Aug92  e  for undo...
  1332.                     eTeEdGuts( hE, chars, (long )offset,
  1333.                                  ( chars[0] == RETURN ? TRUE : FALSE ), TRUE, style );
  1334.                     */
  1335.                     eTeKeyIns( hE, chars[0], style );
  1336.                     /* (**hE).maxRight = (**hE).caretRect.right;
  1337.                        -- done in eTeEdGuts --                   02Oct92  e  */
  1338.                     break;
  1339.             } /* END SWITCH ASCII CHARACTER */
  1340.  
  1341.             /* Update selection flag and turn cursor back on */
  1342.             (**hE).selActive = ( PTL( (**hE).selStart ) != PTL( (**hE).selEnd ) );
  1343.             eTeSetCaretState( hE, CARET_ON );
  1344.             HUnlock( (Handle )(**hE).hLines );
  1345.             break;
  1346.     }
  1347. }
  1348.  
  1349. void eTeClick( eRec **hE, Point hitPt, short modifierKeys, long when )
  1350. {
  1351.     ChPos    pos;
  1352.     Point    newPt;
  1353.     Boolean didScroll = TRUE;
  1354.     long    low, high, offset, start, end, anchorStart, anchorEnd;
  1355.     eRec *pE;
  1356.     
  1357.     HLock( (Handle )hE );
  1358.     pE = *hE;
  1359.  
  1360.     if ( ( hE == gLastViewHit )  &&
  1361.          ( ( when - gLastMouseUp.when ) < GetDblTime() )  &&
  1362.          ( Abs ( gLastMouseDown.where.h - hitPt.h ) < 3 ) &&
  1363.          ( Abs ( gLastMouseDown.where.v - hitPt.v ) < 3 ) )
  1364.         gClicks++;
  1365.     else
  1366.         gClicks = 1;
  1367.     gLastViewHit = hE;
  1368.  
  1369.     eTePrepare( hE );
  1370.  
  1371.     eTeSetCaretState( hE, CARET_OFF );
  1372.  
  1373.     if ( gClicks > 3 )
  1374.         gClicks = 3;
  1375.  
  1376.     if ( modifierKeys & shiftKey ) {
  1377.         gClicks = 1;
  1378.         if ( ! (*pE).selActive ) {
  1379.             (*pE).selStart = (*pE).caretChPos;
  1380.             (*pE).selEnd = (*pE).selStart;
  1381.             (*pE).selActive = TRUE;
  1382.         }
  1383.         start = eTeChPosToOffset( hE, (*pE).selStart );
  1384.         end = eTeChPosToOffset( hE, (*pE).selEnd );
  1385.         /*  24Jul92  e  was...
  1386.         if( modifierKeys & optionKey )
  1387.             anchorStart = anchorEnd = end;
  1388.         else
  1389.             anchorStart = anchorEnd = start;
  1390.         s.b.... */
  1391.         if( eTeChPosToOffset( hE, (*pE).caretChPos ) == start )
  1392.             anchorStart = anchorEnd = (long )( ( modifierKeys & optionKey ) ? start : end );
  1393.         else
  1394.             anchorStart = anchorEnd = (long )( ( modifierKeys & optionKey ) ? end : start );
  1395.         /* since caretChPos is the active end    24Jul92  e  */
  1396.     }
  1397.     else if ( (*pE).selActive ) {
  1398.         eTeHiliteRange( hE, (*pE).selStart, (*pE).selEnd );
  1399.         (*pE).selActive = FALSE;
  1400.     }
  1401.  
  1402.     /*
  1403.      * The DO-WHILE loop monitors the cursor while the mouse button is held down.
  1404.      * The selection is modified with changes in the cursor. NewPt is the new
  1405.      * mouse location, while hitPt contains the last position.
  1406.      */
  1407.     newPt = hitPt;
  1408.     do {
  1409.         if ( didScroll || PTL( newPt ) != PTL( hitPt ) ) {
  1410.             /* first time OR did scroll last loop OR mouse has moved */
  1411.             /* (didScroll == TRUE) causes this to execute first time */
  1412.             switch ( gClicks ) {
  1413.                 case 3:
  1414.                     pos = eTePointToChPos( hE, newPt );
  1415.                     pos.h = 0;
  1416.                     low = eTeChPosToOffset( hE, pos );
  1417.                     if ( pos.v < (*pE).bounds.v - 1 ) {
  1418.                         ++pos.v;
  1419.                         pos.h = 0;
  1420.                     }
  1421.                     else {
  1422.                         pos.h = (*(*pE).hLines)[ pos.v + 1 ] - (*(*pE).hLines)[ pos.v ];
  1423.                     }
  1424.                     high = eTeChPosToOffset( hE, pos );
  1425.                     break;
  1426.  
  1427.                 case 2:
  1428.                     offset = eTeChPosToOffset( hE, eTePointToChPos( hE, newPt ) );
  1429.                     low = WordLimits( *(*pE).hText, offset, TRUE );
  1430.                     high = WordLimits( *(*pE).hText, offset, FALSE );
  1431.                     break;
  1432.  
  1433.                 default:
  1434.                     low = eTeChPosToOffset( hE, eTePointToChPos( hE, newPt ) );
  1435.                     high = low;
  1436.                     break;
  1437.             }
  1438.             /*
  1439.              * Use selActive variable to tell us if this is the first time thru.
  1440.              * If so, then set our anchor points.
  1441.              * Otherwise we just update the selection around the anchors.
  1442.              */
  1443.             if ( ! (*pE).selActive ) {
  1444.                 start = anchorStart = low;
  1445.                 end = anchorEnd = high;
  1446.                 eTeHiliteRange( hE, eTeOffsetToChPos( hE, low ), eTeOffsetToChPos( hE, high ) );
  1447.                 (*pE).selActive = TRUE;
  1448.             }
  1449.             if ( low < start ) {
  1450.                 eTeHiliteRange( hE, eTeOffsetToChPos( hE, low ), eTeOffsetToChPos( hE, start ) );
  1451.                 start = low;
  1452.             }
  1453.             else if ( low < anchorStart ) {
  1454.                 eTeHiliteRange( hE, eTeOffsetToChPos( hE, start ), eTeOffsetToChPos( hE, low ) );
  1455.                 start = low;
  1456.             }
  1457.             else {
  1458.                 eTeHiliteRange( hE, eTeOffsetToChPos( hE, start ), eTeOffsetToChPos( hE, anchorStart ) );
  1459.                 start = anchorStart;
  1460.             }
  1461.             if ( high > end ) {
  1462.                 eTeHiliteRange( hE, eTeOffsetToChPos( hE, end ), eTeOffsetToChPos( hE, high ) );
  1463.                 end = high;
  1464.             }
  1465.             else if ( high > anchorEnd ) {
  1466.                 eTeHiliteRange( hE, eTeOffsetToChPos( hE, high ), eTeOffsetToChPos( hE, end ) );
  1467.                 end = high;
  1468.             }
  1469.             else {
  1470.                 eTeHiliteRange( hE, eTeOffsetToChPos( hE, anchorEnd ), eTeOffsetToChPos( hE, end ) );
  1471.                 end = anchorEnd;
  1472.             }
  1473.             (*pE).selStart = eTeOffsetToChPos( hE, start );
  1474.             (*pE).selEnd = eTeOffsetToChPos( hE, end );
  1475.         }
  1476.         hitPt = newPt;
  1477.         GetMouse( &newPt );
  1478.         didScroll = eTeAutoScroll( hE, newPt );
  1479.     } while ( StillDown() );
  1480.  
  1481.     if ( start == end ) {
  1482.         /*
  1483.         (*pE).caretChPos = eTeOffsetToChPos( hE, start );
  1484.         (*pE).selActive = FALSE;
  1485.         (*pE).selStart = (*pE).caretChPos;
  1486.         (*pE).selEnd = (*pE).caretChPos;
  1487.         */
  1488.         (*pE).selActive = FALSE;
  1489.         (*pE).caretChPos = (*pE).selStart;
  1490.     }
  1491.     else {
  1492.         if( start == anchorStart )
  1493.             (*pE).caretChPos = (*pE).selEnd;
  1494.         else
  1495.             (*pE).caretChPos = (*pE).selStart;
  1496.     }
  1497.     eTeUpdateCaretRect( hE );
  1498.     (*pE).maxRight = (*pE).caretRect.right;
  1499.     eTeSetCaretState( hE, CARET_ON );
  1500.     HUnlock( (Handle )hE );
  1501. }
  1502.  
  1503. /* 2May92  e  -- Style */
  1504.  
  1505. static short eTeOffsetToRun( eRec **hE, long anOffset )
  1506. {
  1507.     register long *runPtr;
  1508.     register long qRuns, count, j;
  1509.  
  1510.     if ( anOffset < 0 )  return -1;    /* special case for j == 0 below */
  1511.     if ( anOffset == 0 )  return 0;    /* special case for empty text */
  1512.  
  1513.     runPtr = *(**hE).hRuns;
  1514.     count = 0;
  1515.     qRuns = (**hE).qRuns;
  1516.     while ( count < qRuns )    {
  1517.         j = (count + qRuns - 1) >> 1;
  1518.         if( anOffset < runPtr[ j ] )
  1519.             qRuns = j;
  1520.         else if( anOffset >= runPtr[ j + 1 ] )
  1521.             count = j + 1;
  1522.         else    {
  1523.             count = j;
  1524.             break;
  1525.         }
  1526.     }
  1527.     return count;
  1528. }
  1529.  
  1530. static short eTeRunToStyle( eRec **hE, short r )
  1531. {
  1532.     return ( ( r & 1 ) ^ (**hE).fustStyle );
  1533. }
  1534.  
  1535. /* 12Sep92  e  */
  1536. void eTeGetRun( eRec **hE, long *sta, long *end )
  1537. {
  1538.     long car = eTeChPosToOffset( hE, (**hE).caretChPos );
  1539.     short run = eTeOffsetToRun( hE, car-1 );
  1540.     short sty = eTeRunToStyle( hE, run );
  1541.     if( run < 0 || sty == 0 )
  1542.       *sta = *end = car;
  1543.     else
  1544.     { long *runVec = *(**hE).hRuns;
  1545.       *sta = runVec[run];
  1546.       *end = runVec[run+1];
  1547.     }
  1548. }
  1549.  
  1550. static void eTeUpdateRuns( eRec **hE, long j, long k, long n, short ns )
  1551. {    /* removing chars j..k, inserting n chars of style ns */
  1552.     short jr = eTeOffsetToRun( hE, j - 1 );
  1553.     short kr = eTeOffsetToRun( hE, k );
  1554.     short js = eTeRunToStyle( hE, jr );
  1555.     short ks = eTeRunToStyle( hE, kr );
  1556.     long adj = n - ( k - j );
  1557.     long *runVec = *(**hE).hRuns;
  1558.     short qRuns = (**hE).qRuns;
  1559.     
  1560.     if( n == 0 ) ns = ks;
  1561.     
  1562.     if( ( ns != js ) || ( j == 0 ) )
  1563.     {    /* can't merge n with j */
  1564.         if( j == 0 ) (**hE).fustStyle = ns;
  1565.         /* 10Aug92  e  was: 
  1566.         if( (( n != 0 ) && ( kr == qRuns )) || (( ns != ks ) && ( kr <= jr + 1 )) ) */
  1567.         if( ( n != 0 ) && ( kr <= jr + 1 ) && ( kr == qRuns || ns != ks ) )
  1568.         {    /* grow */
  1569.             short indx, incr = ( ( j == 0 ) || ( kr == qRuns ) ) ? 1 : 2;
  1570.             qRuns += incr;
  1571.             if ( qRuns >= (**hE).runsAllocated )
  1572.             {    (**hE).runsAllocated += MIN_RUNS;
  1573.                 SetHandleSize( (Handle )(**hE).hRuns, (**hE).runsAllocated * sizeof( long ) );
  1574.                 runVec = *(**hE).hRuns;
  1575.             }
  1576.             kr = jr + incr;
  1577.             for( indx = qRuns; indx > kr; indx-- )
  1578.                 runVec[indx] = runVec[indx-incr] + adj;
  1579.             runVec[++jr] = j;
  1580.             if( jr != kr ) runVec[++jr] = j + n;
  1581.             (**hE).qRuns = qRuns;
  1582.             return;
  1583.         }
  1584.         runVec[++jr] = j;
  1585.     }
  1586. #ifdef use_old_buggy_code
  1587.     /* 16Sep92  e  was... */
  1588.     else if( ( kr == qRuns ) && ( ns == ks ) /* && ( ns == js ) */ )
  1589.         kr--;
  1590.     if( ns == ks )
  1591. #endif
  1592.     if( ns == ks && ( kr != qRuns || ns != js ) )    /* 16Sep92  e */
  1593.     {    /* merge k with n (1&3) */
  1594.         while( kr < qRuns ) runVec[++jr] = runVec[++kr] + adj;
  1595.     }
  1596.     else
  1597.     {    /* can't merge k with n (2&4) */
  1598.         if( k > runVec[kr] ) { runVec[++jr] = j + n; kr++; }    /*  13Oct92  e  */
  1599.         while( kr <= qRuns ) runVec[++jr] = runVec[kr++] + adj;
  1600.     }
  1601.     (**hE).qRuns = jr;
  1602.     /* see if we should shrink the runs vector */
  1603.     if ( jr + MIN_RUNS + MIN_RUNS < (**hE).runsAllocated )
  1604.     {    (**hE).runsAllocated = ( jr / MIN_RUNS + 1 ) * MIN_RUNS;
  1605.         SetHandleSize( (Handle )(**hE).hRuns, (**hE).runsAllocated * sizeof( long ) );
  1606.     }
  1607. }
  1608.  
  1609. /* ******** */
  1610.  
  1611. static short eTeUpdateLineStarts( eRec **hE, short firstLine )
  1612. {
  1613.     short error = noErr;
  1614.     register LineRec     *linePtr;
  1615.     register char        *charPtr, c;
  1616.     register short        numLines;
  1617.     register long        maxLine;    /* 28May92  e */
  1618.     register long        offsLine;    /* 28May92  e */
  1619.     register short        wrap;        /*  5Jul92  e */
  1620.     eRec    *pE;
  1621.     char    *pT;
  1622.  
  1623.     MoveHHi( (Handle )hE );
  1624.     HLock( (Handle )hE );
  1625.     pE = *hE;
  1626.     MoveHHi( (*pE).hText );
  1627.     HLock( (*pE).hText );
  1628.     pT  = *(*pE).hText;
  1629.     HLock( (Handle )(*pE).hLines );
  1630.  
  1631.     linePtr = *(*pE).hLines;
  1632.     numLines = firstLine + 1;
  1633.     
  1634.     maxLine = (firstLine == 0) ? 0 : (*pE).bounds.h;    /* 28May92  e  */
  1635.     wrap = (*pE).wrap;                                    /*  5Jul92  e  */
  1636.  
  1637.     /* Scan entire block of text, start with line firstLine */
  1638.     for ( charPtr = linePtr[ firstLine ] + pT; ( c = *charPtr ) != '\0';  ++charPtr )
  1639.     {    /* Found end of line. Store next line's starting position */
  1640.         if ( c == RETURN
  1641.              || ( --wrap <= 0 && (c = charPtr[1]) != RETURN && c != '\0' ) )
  1642.              /* 10Aug92  e  added RETURN test  |  11Aug92  e  added '\0' test  */
  1643.         {    wrap = (*pE).wrap;                                    /*  5Jul92  e  */
  1644.             offsLine = charPtr - pT + 1;
  1645.             if( ( offsLine - linePtr[firstLine] ) >= maxLine )    /* 10Aug92  e  was > */
  1646.                 maxLine = offsLine - linePtr[firstLine] + 1;    /* 10Aug92  e  +1 */
  1647.             linePtr[ ++firstLine ] = offsLine;
  1648.             /* */
  1649.             ++numLines;
  1650.             /* See if we need to allocate more space for our line starts */
  1651.             if ( numLines >= (*pE).linesAllocated )
  1652.             {    (*pE).linesAllocated += MIN_LINES;
  1653.                 HUnlock( (Handle )(*pE).hLines );
  1654.                 SetHandleSize( (Handle )(*pE).hLines, (*pE).linesAllocated * sizeof( LineRec ) );
  1655.                   error = LMGetMemErr(); // asm { move.w D0, error } /* error = MemError(); */
  1656.                   if( error != noErr )
  1657.                   {    HUnlock( (*pE).hText );
  1658.                       HUnlock( (Handle )hE );
  1659.                       return error;
  1660.                   }
  1661.                 MoveHHi( (Handle )(*pE).hLines );
  1662.                 HLock( (Handle )(*pE).hLines );
  1663.                 linePtr = *(*pE).hLines;
  1664.             }
  1665.         }
  1666.     }
  1667.     /* Last entry contains length of entire text block */
  1668.     offsLine = charPtr - pT;
  1669.     if( ( offsLine - linePtr[firstLine] ) >= maxLine )            /* 10Aug92  e  was > */
  1670.         maxLine = offsLine - linePtr[firstLine] + 1;            /* 10Aug92  e  +1 */
  1671.     linePtr[ firstLine + 1 ] = offsLine;
  1672.  
  1673.     HUnlock( (*pE).hText );
  1674.     HUnlock( (Handle )(*pE).hLines );
  1675.  
  1676.     /* See if we should shrink our line starts block */
  1677.     if ( numLines + MIN_LINES < (*pE).linesAllocated ) {
  1678.         (*pE).linesAllocated = ( numLines / MIN_LINES + 1 ) * MIN_LINES;
  1679.         SetHandleSize( (Handle )(*pE).hLines, (*pE).linesAllocated * sizeof( LineRec ) );
  1680.           error = LMGetMemErr(); // asm { move.w D0, error } /* error = MemError(); */
  1681.     }
  1682.  
  1683.     (*pE).bounds.h = Min( maxLine, 32767 );  /* 28May92  e  */
  1684.     (*pE).bounds.v = numLines;
  1685.     HUnlock( (Handle )hE );
  1686.     eTeAdjustScrollMax( hE );
  1687.     return error;
  1688. }
  1689.  
  1690. static short eTeSetTextGuts( eRec** hE, Handle hT, long numChars )
  1691. {    short error = noErr;
  1692.     if ( (**hE).hText )
  1693.         DisposeHandle( (**hE).hText );
  1694.     if ( (*hT)[ numChars -1 ] != 0 ) {
  1695.         numChars += 1;
  1696.         SetHandleSize( hT, numChars );
  1697.           error = LMGetMemErr(); // asm { move.w D0, error } /* error = MemError(); */
  1698.           if( error != noErr ) return error;
  1699.     }
  1700.     (*hT)[ numChars - 1 ] = '\0';
  1701.     (**hE).hText = hT;
  1702.     error = eTeUpdateLineStarts( hE, 0 );
  1703.       if( error != noErr ) return error;
  1704.     error = eTeNewRuns( hE, numChars - 1 );
  1705.       if( error != noErr ) return error;
  1706.     (**hE).caretChPos.h = (**hE).caretChPos.v = 0L;
  1707.     (**hE).selStart = (**hE).selEnd = (**hE).caretChPos;
  1708.     (**hE).maxRight = 0;
  1709.     eTeUpdateCaretRect( hE );
  1710.     (**hE).dirty = FALSE;
  1711.     eTeRefresh( hE );                            /* 17Dec92  e  */
  1712.     return error;
  1713. }
  1714.  
  1715.  
  1716. short eTeSetTextHandleDetabify( eRec **hE, Handle hT, short tabstops )
  1717. {    long count;
  1718.     long size = GetHandleSize( hT );
  1719.     short error = DeTabifyHandle( hT, &size, &count, tabstops >= 0 ? tabstops : (**hE).tabStops );
  1720.     if ( error != noErr ) return error;
  1721.     /* see if count lines fit in lineStarts array */
  1722.     if( count > ( 32767 - ( MIN_LINES >> 1) ) )
  1723.         return -357;
  1724.     if ( count >= (**hE).linesAllocated )
  1725.     {    (**hE).linesAllocated = count + ( MIN_LINES >> 1);
  1726.         SetHandleSize( (Handle )(**hE).hLines, (**hE).linesAllocated * sizeof( LineRec ) );
  1727.           error = LMGetMemErr(); // asm { move.w D0, error } /* error = MemError(); */
  1728.           if( error != noErr )
  1729.           {    (**hE).linesAllocated = 0;
  1730.               return error;
  1731.           }
  1732.     }
  1733.     return eTeSetTextGuts( hE, hT, size );
  1734. }
  1735.  
  1736. short eTeSetTextHandle( eRec** hE, Handle hT )
  1737. {
  1738.     return eTeSetTextGuts( hE, hT, GetHandleSize( hT ) );
  1739. }
  1740.  
  1741. short eTeSetTextPtr( eRec** hE, Ptr pT, long numChars )
  1742. {
  1743.     Handle hT;
  1744.     
  1745.     if ( pT[ numChars -1 ] != 0 )
  1746.         numChars += 1;
  1747.     PtrToHand( pT, &hT, numChars );
  1748.     (*hT)[ numChars - 1 ] = '\0';
  1749.     return eTeSetTextGuts( hE, hT, numChars );
  1750. }
  1751.  
  1752. void eTeSetSelect( eRec **hE, long aStart, long anEnd )
  1753. {
  1754.     ChPos    first, last;
  1755.     /* Unhilite existing selection */
  1756.     if ( (**hE).selActive ) {
  1757.         first = (**hE).selStart;
  1758.         last = (**hE).selEnd;
  1759.         eTeHiliteRange( hE, first, last );
  1760.         (**hE).selActive = FALSE;
  1761.     }
  1762.     else
  1763.         eTeSetCaretState( hE, CARET_OFF );
  1764.  
  1765.     if ( aStart < 0 ) aStart = 0;
  1766.     if ( anEnd < 0 ) anEnd = 0;
  1767.     if ( aStart >= anEnd ) {
  1768.         (**hE).selStart = (**hE).selEnd = eTeOffsetToChPos( hE, aStart );
  1769.     }
  1770.     else {
  1771.         (**hE).selActive = TRUE;
  1772.         first = eTeOffsetToChPos( hE, aStart );
  1773.         last = eTeOffsetToChPos( hE, anEnd );
  1774.         (**hE).selStart = first;
  1775.         (**hE).selEnd = last;
  1776.         eTeHiliteRange( hE, first, last );
  1777.     }
  1778.     (**hE).caretChPos = (**hE).selEnd;
  1779.     eTeUpdateCaretRect( hE );
  1780.     (**hE).maxRight = (**hE).caretRect.right;
  1781. }
  1782.  
  1783. static Boolean eTeHasReturns( register char *data, register long len )
  1784. {
  1785.     while( len-- )
  1786.         if ( *data++ == RETURN )
  1787.             return TRUE;
  1788.     return FALSE;
  1789. }
  1790.  
  1791. /* the scrap  13Aug92  e  */
  1792.  
  1793. void     eTePutScrap()
  1794. {    OSErr        err;
  1795.  
  1796.     HLock( eTeScrap );
  1797.     /* Make a copy of the scrap and give to the clipboard */
  1798.     if( ( err = ZeroScrap() ) == noErr )
  1799.     {    err = PutScrap( eTeScrapLen, 'TEXT', *eTeScrap );
  1800.     }
  1801.     if( err == noErr )
  1802.     {    
  1803.     }
  1804.     else SysBeep( 3 );
  1805.     eTeScrapCnt = (InfoScrap())->scrapCount;
  1806.     HUnlock( eTeScrap );
  1807. }
  1808.  
  1809. static void puntUndoStuff()
  1810. {
  1811.     eTeUndoStuff.undoTeRec = NULL;
  1812.     eTeUndoStuff.undoTitle = utUndo;
  1813.     eTeUndoStuff.undoProc  = undoBugNi;
  1814. }
  1815.  
  1816. void     eTeGetScrap()
  1817. {    long offset, length;
  1818.  
  1819.     if( eTeScrapCnt != (InfoScrap())->scrapCount )
  1820.     {    length = GetScrap( NULL, 'TEXT', &offset );
  1821.         if ( length >= 0 )
  1822.         { eTeScrapLen = GetScrap( eTeScrap, 'TEXT', &offset );
  1823.           puntUndoStuff();
  1824.         }
  1825.         else if( length != noTypeErr )
  1826.         { SysBeep( 3 );
  1827.           SetHandleSize( eTeScrap, 0 );
  1828.           eTeScrapLen = 0;
  1829.           puntUndoStuff();
  1830.         }
  1831.         /* else punt: noTypeErr => nothing to copy */
  1832.     }
  1833. }
  1834.  
  1835. /* I don't feel bad using a large buffer on the stack,
  1836.    even on a MacPlus Quickdraw uses several K of stack space to draw text! */
  1837. #define TR_BUF_SZ 4000
  1838.  
  1839. /* ####################################################################
  1840.  
  1841. 14Aug92  e
  1842.  
  1843. Undo notes...
  1844.  
  1845. try to alloc a temp handle in dodoTyping()
  1846.  
  1847. concatenate successive kills (use clip or undo ?) ?
  1848.  
  1849. add to eTeKeyIns() mechanism for delete and del>
  1850.  
  1851.   if undoTyping or undoClear already active && there's no selection
  1852.  
  1853.     if normal char
  1854.       if caret >= undoStart && caret <= undoStart + insertLen
  1855.       increment insertLen
  1856.       insert char
  1857.     else
  1858.       start new undo & keyAccum
  1859.  
  1860.     if Delete
  1861.       if caret > undoStart && caret <= undoStart + insertLen
  1862.         decr insertLen
  1863.         do the delete
  1864.       else if caret == undoStart
  1865.         put character at caret-1 onto front of undoText
  1866.         do the delete
  1867.       else
  1868.         start new undo & keyAccum
  1869.  
  1870.     if Del>
  1871.       if caret >= undoStart && caret < undoStart + insertLen
  1872.         decr insertLen
  1873.         do the delete
  1874.       else if caret == undoStart + insertLen
  1875.         put character at caret onto end of undoText
  1876.         do the delete
  1877.       else
  1878.         start new undo & keyAccum
  1879.  
  1880.   else
  1881.     what's done now
  1882.  
  1883. ensureSelOK() is only necessary
  1884.  because caretChPos is not kept syncronized with selStart & selEnd and vice versa
  1885.  should this be fixed so ensureSelOK() can be eliminated?  for other reasons?
  1886.  
  1887. make eTeTranspose() work better with undo -- at least adjust offsets?
  1888.  
  1889. subroutinize undo routines
  1890.   -- eTeEdGuts( hE, EMPTY_PTR, 0L, FALSE, TRUE, 0 ); ??
  1891.   -- hoseUndoText();
  1892.   -- rsSelectionPts();
  1893.  
  1894. /* ##################### */
  1895.  
  1896. static void ensureSelOK( eRec **hE )                /* can this be eliminated !? */
  1897. {    if( ! (**hE).selActive )
  1898.         (**hE).selStart = (**hE).selEnd = (**hE).caretChPos;
  1899. }
  1900.  
  1901. void eTeUndo( eRec **hE )
  1902. {
  1903.     if( hE == eTeUndoStuff.undoTeRec )
  1904.         eTeUndoStuff.undoProc( hE );
  1905.     else SysBeep( 3 );
  1906. }
  1907.  
  1908. void eTeEditMenuUpdate( eRec **hE, MenuHandle hM )
  1909. {
  1910.     SetMenuItemText( hM, 1, eTeUndoStuff.undoTitle );        /* memoize this ?! */
  1911.     if( hE != NULL )
  1912.     {    if( (**hE).selActive )
  1913.         {    EnableItem( hM, 3 ); /* cut */
  1914.             EnableItem( hM, 4 ); /* copy */
  1915.         }
  1916.         else
  1917.         {    DisableItem( hM, 3 ); /* cut */
  1918.             DisableItem( hM, 4 ); /* copy */
  1919.         }
  1920.         EnableItem( hM, 5 ); /* paste */
  1921.         EnableItem( hM, 6 ); /* clear */
  1922.         if( hE == eTeUndoStuff.undoTeRec )
  1923.             EnableItem( hM, 1 );
  1924.         else
  1925.             DisableItem( hM, 1 ); /* undo */
  1926.     }
  1927.     else
  1928.     {    EnableItem( hM, 3 ); /* cut */
  1929.         EnableItem( hM, 4 ); /* copy */
  1930.         EnableItem( hM, 5 ); /* paste */
  1931.         EnableItem( hM, 6 ); /* clear */
  1932.         DisableItem( hM, 1 ); /* undo */
  1933.     }
  1934. }
  1935.  
  1936. static void ssSelectionPts( eRec **hE )
  1937. {    ensureSelOK( hE );
  1938.     eTeUndoStuff.undoInLen = 0;
  1939.     eTeUndoStuff.undoKeyAccum = FALSE;
  1940.     eTeUndoStuff.undoStart = eTeChPosToOffset( hE, (**hE).selStart );
  1941.     eTeUndoStuff.undoEnd   = eTeChPosToOffset( hE, (**hE).selEnd   );
  1942.     eTeUndoStuff.undoCarStaP = PTL( (**hE).caretChPos ) == PTL( (**hE).selStart );
  1943.     eTeUndoStuff.undoDirty = (**hE).dirty;
  1944.     eTeUndoStuff.undoTeRec = hE;
  1945. }
  1946.  
  1947. static void ssSwapScrap()
  1948. {    Handle h;
  1949.     long l;
  1950.     h = eTeScrap;
  1951.     l = eTeScrapLen;
  1952.     eTeScrap = eTeUndoStuff.undoText;
  1953.     eTeScrapLen = eTeUndoStuff.undoTxLen;
  1954.     eTeUndoStuff.undoText = h;
  1955.     eTeUndoStuff.undoTxLen = l;
  1956. }
  1957.  
  1958. static void eTeSetSelCar( eRec **hE, long aStart, long anEnd, Boolean stap )
  1959. {
  1960.     eTeSetSelect( hE, aStart, anEnd );
  1961.     if( stap ) (**hE).caretChPos = (**hE).selStart;
  1962. }
  1963.  
  1964. static void eTePasteNu( eRec **hE, short style )
  1965. {
  1966.     MoveHHi( eTeScrap );
  1967.     HLock( eTeScrap );
  1968.     eTeEdGuts( hE, *eTeScrap, eTeScrapLen, eTeHasReturns( *eTeScrap, eTeScrapLen ), TRUE, style );
  1969.     HUnlock( eTeScrap );
  1970. }
  1971.  
  1972. static void eTeCopyNu( eRec **hE )
  1973. {
  1974.     long        length, offset;
  1975.  
  1976.     /* Get starting position and length of text to copy */
  1977.     offset = eTeChPosToOffset( hE, (**hE).selStart );
  1978.     length = eTeChPosToOffset( hE, (**hE).selEnd ) - offset;
  1979.     MoveHHi( (**hE).hText );
  1980.     HLock( (**hE).hText );
  1981.     /* Make a copy of the text and put in scrap */
  1982.     offset = Munger( eTeScrap, 0L, NULL, eTeScrapLen, *(**hE).hText + offset, length );
  1983.     if( offset == length )
  1984.         eTeScrapLen = length; /* success */
  1985.     else SysBeep( 3 );
  1986.     HUnlock( (**hE).hText );
  1987. }
  1988.  
  1989. static void undoCut( eRec  **hE )                /* Undo Cut */
  1990. {                                                /* restore selection from scrap */
  1991.     eTeSetSelect( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoStart );
  1992.     eTePasteNu( hE, 0 );
  1993.     ssSwapScrap();                                 /* restore scrap */
  1994.     SetHandleSize( eTeUndoStuff.undoText, 0 );
  1995.     eTeUndoStuff.undoTxLen = 0;
  1996.     eTeSetSelCar( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoEnd, eTeUndoStuff.undoCarStaP );
  1997.     (**hE).dirty = eTeUndoStuff.undoDirty;        /* restore selection points & flags */
  1998.     eTeUndoStuff.undoTitle = rtCut;
  1999.     eTeUndoStuff.undoProc  = eTeCut;
  2000. }
  2001.   
  2002. void eTeCut( eRec **hE )                        /*  Cut == RedoCut  */
  2003. {    if( ! (**hE).selActive ) return;
  2004.     ssSelectionPts( hE );  /* snapshot selection points */
  2005.     ssSwapScrap();         /* snapshot scrap */
  2006.     eTeUndoStuff.undoTitle = utCut;
  2007.     eTeUndoStuff.undoProc  = undoCut;
  2008.     eTeCopyNu( hE );                            /* do cut */
  2009.     eTeEdGuts( hE, EMPTY_PTR, 0L, FALSE, TRUE, 0 );
  2010. }
  2011.  
  2012. static void undoCopy( eRec  **hE )                /* Undo Copy */
  2013. {    
  2014.     ssSwapScrap();                                 /* restore scrap */
  2015.     SetHandleSize( eTeUndoStuff.undoText, 0 );
  2016.     eTeUndoStuff.undoTxLen = 0;
  2017.     eTeSetSelCar( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoEnd, eTeUndoStuff.undoCarStaP );
  2018.     (**hE).dirty = eTeUndoStuff.undoDirty;        /* restore selection points & flags */
  2019.     eTeUndoStuff.undoTitle = rtCopy;
  2020.     eTeUndoStuff.undoProc  = eTeCopy;
  2021. }
  2022.   
  2023. void eTeCopy( eRec **hE )                        /* Copy == Redo Copy */
  2024. {    if( ! (**hE).selActive ) return;
  2025.     ssSelectionPts( hE );                          /* snapshot selection points */
  2026.     ssSwapScrap();                                 /* snapshot scrap */
  2027.     eTeUndoStuff.undoTitle = utCopy;
  2028.     eTeUndoStuff.undoProc  = undoCopy;
  2029.     eTeCopyNu( hE );                            /* do copy */
  2030. }
  2031.  
  2032. static void redoPaste( eRec  **hE )                /* Redo Paste */
  2033. {    eTePaste( hE, 0 );
  2034. }
  2035.  
  2036. static void undoPaste( eRec  **hE )                /* Undo Paste */
  2037. {                                                /* restore selection from scrap */
  2038.     eTeSetSelect( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoStart + eTeScrapLen );
  2039.     ssSwapScrap();                                 /* swap scrap & undo */
  2040.     eTePasteNu( hE, 0 );                        /* restore text from undo */
  2041.     ssSwapScrap();                                 /* swap scrap & undo */
  2042.     SetHandleSize( eTeUndoStuff.undoText, 0 );
  2043.     eTeUndoStuff.undoTxLen = 0;
  2044.     eTeSetSelCar( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoEnd, eTeUndoStuff.undoCarStaP );
  2045.     (**hE).dirty = eTeUndoStuff.undoDirty;        /* restore selection points & flags */
  2046.     eTeUndoStuff.undoTitle = rtPaste;
  2047.     eTeUndoStuff.undoProc  = redoPaste;
  2048. }
  2049.  
  2050. void eTePaste( eRec **hE, short style )            /* Paste != Redo Paste */
  2051. {    ssSelectionPts( hE );                          /* snapshot selection points */
  2052.     ssSwapScrap();                                 /* snapshot selection */
  2053.     eTeCopyNu( hE );
  2054.     ssSwapScrap();                                 /* restore scrap */
  2055.     eTeUndoStuff.undoTitle = utPaste;
  2056.     eTeUndoStuff.undoProc  = undoPaste;
  2057.     eTePasteNu( hE, style );                    /* do paste [14Sep92  e  -- added style back] */
  2058. }
  2059.  
  2060. static void undoDelete( eRec **hE )                /* Undo Clear */
  2061. {                                                /* restore selection from scrap */
  2062.     eTeSetSelect( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoStart );
  2063.     ssSwapScrap();                                 /* swap scrap & undo */
  2064.     eTePasteNu( hE, 0 );                        /* restore text from undo */
  2065.     ssSwapScrap();                                 /* swap scrap & undo */
  2066.     SetHandleSize( eTeUndoStuff.undoText, 0 );
  2067.     eTeUndoStuff.undoTxLen = 0;
  2068.     eTeSetSelCar( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoEnd, eTeUndoStuff.undoCarStaP );
  2069.     (**hE).dirty = eTeUndoStuff.undoDirty;        /* restore selection points & flags */
  2070.     eTeUndoStuff.undoTitle = rtClear;
  2071.     eTeUndoStuff.undoProc  = eTeDelete;
  2072. }
  2073.  
  2074. void eTeDelete( eRec **hE )                        /* Clear == Redo Clear */
  2075. {    long offset;
  2076.     if ( ! (**hE).selActive )
  2077.     {    /* Just delete previous character */
  2078.         offset = eTeChPosToOffset( hE, (**hE).caretChPos );
  2079.         if ( offset <= 0 ) return;
  2080.         ssSelectionPts( hE );                          /* snapshot selection points */
  2081.         eTeUndoStuff.undoStart--;
  2082.         (**hE).selEnd = (**hE).caretChPos;
  2083.         (**hE).selStart = eTeOffsetToChPos( hE, --offset );
  2084.     }
  2085.     else
  2086.         ssSelectionPts( hE );                          /* snapshot selection points */
  2087.     ssSwapScrap();                                 /* snapshot selection */
  2088.     eTeCopyNu( hE );
  2089.     ssSwapScrap();                                 /* restore scrap */
  2090.     eTeUndoStuff.undoTitle = utClear;
  2091.     eTeUndoStuff.undoProc  = undoDelete;
  2092.     if ( (**hE).selActive )
  2093.         eTeEdGuts( hE, EMPTY_PTR, 0L, FALSE, TRUE, 0 );
  2094.     else
  2095.     {    (**hE).selEnd = (**hE).caretChPos = (**hE).selStart;
  2096.         eTeSetCaretState( hE, CARET_OFF ); /* 23Jul92  e */
  2097.         eTeUpdateCaretRect( hE );
  2098.         eTeEdGuts( hE, EMPTY_PTR, -1L, *(*(**hE).hText + offset) == RETURN, TRUE, 0 );
  2099.     }
  2100. }
  2101.  
  2102. static void redoKill( eRec **hE )                /* Redo Kill */
  2103. {
  2104.     eTeSetSelect( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoStart );
  2105.     eTeKillTo( hE, eTeOffsetToChPos( hE, eTeUndoStuff.undoEnd ) );
  2106. }
  2107.  
  2108. static void undoKill( eRec  **hE )                /* Undo Kill */
  2109. {                                                /* restore selection from scrap */
  2110.     eTeSetSelect( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoStart );
  2111. #if Kill_Deletes
  2112.     ssSwapScrap();                                 /* swap scrap & undo */
  2113. #endif
  2114.     eTePasteNu( hE, 0 );
  2115.     ssSwapScrap();                                 /* restore scrap */
  2116.     SetHandleSize( eTeUndoStuff.undoText, 0 );
  2117.     eTeUndoStuff.undoTxLen = 0;
  2118.     eTeSetSelect( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoStart );
  2119.     (**hE).dirty = eTeUndoStuff.undoDirty;        /* restore selection points & flags */
  2120.     eTeUndoStuff.undoTitle = rtKill;
  2121.     eTeUndoStuff.undoProc  = redoKill;
  2122. }
  2123.   
  2124. void eTeKillTo( eRec **hE, ChPos chPos )        /* Kill != Redo Kill */
  2125. {
  2126.     if( PTL( chPos ) == PTL( (**hE).caretChPos ) ) return;
  2127.     (**hE).selStart = (**hE).caretChPos;        /* mung selection points first! */
  2128.     (**hE).selEnd = chPos;
  2129.     (**hE).selActive = TRUE;
  2130.     ssSelectionPts( hE );                          /* snapshot selection points */
  2131.     ssSwapScrap();                                 /* snapshot scrap */
  2132.     eTeCopyNu( hE );
  2133. #if Kill_Deletes
  2134.     ssSwapScrap();                                 /* restore scrap */
  2135. #endif
  2136.     eTeUndoStuff.undoTitle = utKill;
  2137.     eTeUndoStuff.undoProc  = undoKill;
  2138.     /* since we'll redraw the line anyway, eTeHiliteRange isn't necessary!?    */
  2139.     /*  unfortunately, for wrapped lines, it is - otherwise cursor turds; ugh. */
  2140.     /* eTeHiliteRange( hE, (**hE).selStart, (**hE).selEnd );                   */
  2141.     /* instead we'll fake it out by making the window look inactive instead    */
  2142.     (**hE).active = FALSE;
  2143.     eTeEdGuts( hE, EMPTY_PTR, 0L, FALSE, TRUE, 0 );
  2144.     (**hE).active = TRUE;
  2145. }
  2146.  
  2147. static Boolean dodoTyping( eRec **hE )
  2148. {    char buf[TR_BUF_SZ];
  2149.     long sz;
  2150.     register char *p, *q;
  2151.     register long iter = sz = eTeUndoStuff.undoTxLen;;
  2152.     
  2153.     if( iter > 0 || eTeUndoStuff.undoInLen > 0 )
  2154.     {
  2155.         if( eTeUndoStuff.undoInLen > TR_BUF_SZ )
  2156.         {    SysBeep( 6 );                            /* try to alloc a temp handle ? */
  2157.             return FALSE;
  2158.         }
  2159.         q = buf;
  2160.         p = *eTeUndoStuff.undoText;
  2161.         while( iter-- ) *q++ = *p++;
  2162.         eTeSetSelect( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoStart + eTeUndoStuff.undoInLen );
  2163.         ssSwapScrap();                                 /* snapshot insertion */
  2164.         eTeCopyNu( hE );
  2165.         ssSwapScrap();                                 /* restore scrap */
  2166.         eTeEdGuts( hE, buf, sz, eTeHasReturns( buf, sz ), TRUE, 0 );
  2167.         eTeUndoStuff.undoInLen = sz;
  2168.     }
  2169.     return TRUE;
  2170. }
  2171.  
  2172. static void redoTyping( eRec **hE );
  2173.  
  2174. static void undoTyping( eRec **hE )
  2175. {    if( dodoTyping( hE ) )
  2176.     {    eTeUndoStuff.undoKeyAccum = FALSE;
  2177.         eTeSetSelCar( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoEnd, eTeUndoStuff.undoCarStaP );
  2178.         (**hE).dirty = eTeUndoStuff.undoDirty;        /* restore selection points & flags */
  2179.         eTeUndoStuff.undoTitle = rtTyping;
  2180.         eTeUndoStuff.undoProc  = redoTyping;
  2181.     }
  2182. }
  2183.     
  2184. static void redoTyping( eRec **hE )
  2185. {    eTeUndoStuff.undoKeyAccum = TRUE;
  2186.     eTeUndoStuff.undoDirty = (**hE).dirty;
  2187.     if( dodoTyping( hE ) )
  2188.     {    eTeUndoStuff.undoTitle = utTyping;
  2189.         eTeUndoStuff.undoProc  = undoTyping;
  2190.     }
  2191.     else
  2192.     {    eTeUndoStuff.undoKeyAccum = FALSE;
  2193.     }
  2194. }
  2195.  
  2196. static void eTeKeyIns( eRec **hE, char c, short style )
  2197. {
  2198.     if( eTeUndoStuff.undoKeyAccum
  2199.         && hE == eTeUndoStuff.undoTeRec
  2200.         && (**hE).selActive == FALSE
  2201.         && eTeChPosToOffset( hE, (**hE).caretChPos )
  2202.              == eTeUndoStuff.undoStart + eTeUndoStuff.undoInLen
  2203.       )
  2204.     {    eTeUndoStuff.undoInLen += 1;            /* incr numChars inserted */
  2205.     }
  2206.     else
  2207.     {    ssSelectionPts( hE );                      /* snapshot selection points */
  2208.         eTeUndoStuff.undoKeyAccum = TRUE;
  2209.         ssSwapScrap();                             /* snapshot selection */
  2210.         eTeCopyNu( hE );
  2211.         ssSwapScrap();                             /* restore scrap */
  2212.         eTeUndoStuff.undoInLen = 1;                /* remember numChars inserted */
  2213.         eTeUndoStuff.undoTitle = utTyping;
  2214.         eTeUndoStuff.undoProc  = undoTyping;
  2215.     }
  2216.     eTeEdGuts( hE, &c, 1, c == RETURN, TRUE, style );
  2217. }
  2218.  
  2219. void eTeInsert( eRec **hE, Ptr textPtr, long numChars, short style )
  2220. {    if( numChars <= 0 ) { eTeDelete( hE ); return; } /* 08Jan93  e  */
  2221.     ssSelectionPts( hE );                          /* snapshot selection points */
  2222.     eTeUndoStuff.undoKeyAccum = TRUE;
  2223.     ssSwapScrap();                                 /* snapshot selection */
  2224.     eTeCopyNu( hE );
  2225.     ssSwapScrap();                                 /* restore scrap */
  2226.     eTeUndoStuff.undoInLen = numChars;            /* remember numChars inserted */
  2227.     eTeUndoStuff.undoTitle = utInsert;
  2228.     eTeUndoStuff.undoProc  = undoTyping;
  2229.     eTeEdGuts( hE, textPtr, numChars, eTeHasReturns( textPtr, numChars ), TRUE, style );
  2230. }
  2231.  
  2232. /* #################################################################### */
  2233.  
  2234. /* eTeTranspose  22Jul92  e  */
  2235.  
  2236. static void eTeTransposeGuts1( eRec **hE, long beg, long mid, long end )
  2237. { char buf[TR_BUF_SZ];
  2238.   long shift = mid - beg;
  2239.   register char *p, *q;
  2240.   register long incr, iter;
  2241.   
  2242.   while( shift > 0 )
  2243.   { incr = Min( shift, TR_BUF_SZ );
  2244.     shift -= incr;
  2245.     /* */
  2246.     p = *(**hE).hText + beg;
  2247.     q = buf;
  2248.     iter = incr;
  2249.     while( iter-- ) *q++ = *p++;
  2250.     /* */
  2251.     p = *(**hE).hText + beg + incr;
  2252.     q = *(**hE).hText + beg;
  2253.     iter = end - beg - incr;
  2254.     while( iter-- ) *q++ = *p++;
  2255.     /* */
  2256.     p = buf;
  2257.     q = *(**hE).hText + end - incr;
  2258.     iter = incr;
  2259.     while( iter-- ) *q++ = *p++;
  2260.   }
  2261. }
  2262.  
  2263. static void eTeTransposeGuts0( eRec **hE, long beg, long midl, long midr, long end )
  2264. { char buf[TR_BUF_SZ];
  2265.   register char *p, *q;
  2266.   register long iter;
  2267.   
  2268.   q = buf;
  2269.   p = *(**hE).hText + midl;
  2270.   iter = midr - midl;
  2271.   while( iter-- ) *q++ = *p++;
  2272.   p = *(**hE).hText + beg;
  2273.   iter = midl - beg;
  2274.   while( iter-- ) *q++ = *p++;
  2275.   q = *(**hE).hText + beg;
  2276.   p = *(**hE).hText + midr;
  2277.   iter = end - midr;
  2278.   while( iter-- ) *q++ = *p++;
  2279.   p = buf;
  2280.   iter = midr - beg;
  2281.   while( iter-- ) *q++ = *p++;
  2282. }
  2283.  
  2284. static void eTeTransposeGuts2( eRec **hE, long beg, long midl, long midr, long end )
  2285.   if( midr - beg <= TR_BUF_SZ )
  2286.   { eTeTransposeGuts0( hE, beg, midl, midr, end );
  2287.   }
  2288.   else
  2289.   { eTeTransposeGuts1( hE, beg, midl, midr );
  2290.     eTeTransposeGuts1( hE, beg, midr, end );
  2291.   }
  2292. }
  2293.  
  2294. void eTeTranspose( eRec **hE, long beg, long midl, long midr, long end )
  2295. { long offs = beg + end - midr;
  2296.   long numChars = end - beg;
  2297.   char *textPtr;
  2298.   
  2299.   if ( (**hE).selActive
  2300.         || beg  <  0
  2301.         || beg  >= midl
  2302.         || midl >  midr
  2303.         || midr >= end
  2304.         || end  >  eTeTextLength( hE ) )
  2305.  { SysBeep(3);
  2306.    return;
  2307.   }
  2308.   eTeSetCaretState( hE, CARET_OFF );
  2309.   if( midl == midr )
  2310.     eTeTransposeGuts1( hE, beg, midr, end );
  2311.   else
  2312.     eTeTransposeGuts2( hE, beg, midl, midr, end );
  2313.   (**hE).selStart = eTeOffsetToChPos( hE, beg );
  2314.   (**hE).selEnd   = eTeOffsetToChPos( hE, end );
  2315.   (**hE).selActive = TRUE;
  2316.   textPtr = *(**hE).hText + beg;
  2317.   /* fake it out by making the window look inactive */
  2318.   (**hE).active = FALSE;
  2319.   eTeEdGuts( hE, textPtr, numChars, eTeHasReturns( textPtr, numChars ), FALSE, 0 );
  2320.   (**hE).active = TRUE;
  2321.   eTeSetSelect( hE, offs, offs );
  2322.   eTeShowCaret( hE );
  2323.   puntUndoStuff();                 /* all bets off for now    --  17Aug92  e */
  2324. }
  2325.  
  2326. /* eTeWrite  9Jul92  e  */
  2327.  
  2328. void eTeWrite( eRec **hE, Ptr textPtr, long numChars, short style )
  2329. {    /* save selection, etc. */
  2330.     long sta = eTeChPosToOffset( hE, (**hE).selStart );
  2331.     long end = eTeChPosToOffset( hE, (**hE).selEnd );
  2332.     long car = eTeChPosToOffset( hE, (**hE).caretChPos );
  2333.     long pos = eTeChPosToOffset( hE, (**hE).writeChPos );
  2334.     short mxr = (**hE).maxRight;                            /* 10Aug92  e  */
  2335.     Boolean mxrp = mxr != (**hE).caretRect.right;            /* 10Aug92  e  */
  2336.     Boolean selp = (**hE).selActive;
  2337.     Boolean stap = selp && car == sta;
  2338.     if( selp ) eTeHiliteRange( hE, (**hE).selStart, (**hE).selEnd );
  2339.     else       eTeSetCaretState( hE, CARET_OFF );           /*  14Jul92  e  */
  2340.     /* swap caretChPos & writeChPos */
  2341.     (**hE).selActive = FALSE;
  2342.     (**hE).caretChPos = (**hE).writeChPos;
  2343.     eTeUpdateCaretRect( hE );
  2344.     /* kludge for bs -- 17Mar94  e  */
  2345.     if ( numChars == 1 && *textPtr == '\b' && pos > 0 )
  2346.     { numChars = -1;
  2347.       (**hE).caretChPos = eTeOffsetToChPos( hE, --pos );
  2348.       eTeUpdateCaretRect( hE );
  2349.       eTeEdGuts( hE, EMPTY_PTR, numChars, *(*(**hE).hText + pos) == RETURN, ( pos == car ), style );
  2350.     }
  2351.     else /* end of insert                                   --  17Mar94  e  */
  2352.     /* show was always FALSE; changed to ( pos == car )     --  14Jul92  e  */
  2353.     eTeEdGuts( hE, textPtr, numChars, eTeHasReturns( textPtr, numChars ), ( pos == car ), style );
  2354.     /* set writeChPos to caretChPos */
  2355.     (**hE).writeChPos = (**hE).caretChPos;
  2356.     /* restore selection, etc. */
  2357.     if( selp )
  2358.     { if( pos < end )
  2359.       { end += numChars;
  2360.         if( pos <= sta ) sta += numChars;
  2361.       }
  2362.     }
  2363.     else
  2364.     { if( pos <= car ) car += numChars;
  2365.       sta = end = car;
  2366.     }
  2367.     eTeSetSelCar( hE, sta, end, stap );
  2368.     if( mxrp ) (**hE).maxRight = mxr;                        /* 10Aug92  e  */
  2369.     if( hE == eTeUndoStuff.undoTeRec )                        /* 17Aug92  e  */    
  2370.     {    if( pos <= eTeUndoStuff.undoStart )
  2371.         {    eTeUndoStuff.undoEnd   += numChars;
  2372.             eTeUndoStuff.undoStart += numChars;
  2373.         }
  2374.         else if( pos < eTeUndoStuff.undoEnd )
  2375.             puntUndoStuff(); /* all bets off */
  2376.         /* else OK, a write after undo stuff */
  2377.     }
  2378. }
  2379.  
  2380. /* 11Aug92  e   rewritten with eol and sol and new eTeUpdateLineStarts case  */
  2381.  
  2382. static Boolean eTeAdjustLineStarts( eRec **hE, register short start, short amount )
  2383. {
  2384.     register short bot = (**hE).bounds.v;
  2385.     register LineRec *linePtr;
  2386.     register long incr;
  2387.     long llen;
  2388.     long sol, eol;
  2389.     char *pT, c;
  2390.  
  2391.     if ( ( incr = (long )amount ) != 0 ) {
  2392.         /* 28May92  e  was: linePtr = *(**hE).hLines + start + 1; */
  2393.         pT = *(**hE).hText;
  2394.         linePtr = *(**hE).hLines + start;
  2395.         sol = *linePtr++;
  2396.         eol = *linePtr + incr;
  2397.         llen = eol - sol;
  2398.         if( llen > (**hE).bounds.h )
  2399.         { (**hE).bounds.h = Min( llen, ( (**hE).wrap + 1 ) );
  2400.           HLock( (Handle )(**hE).hLines );
  2401.           eTeAdjustScrollMax( hE );
  2402.           HUnlock( (Handle )(**hE).hLines );
  2403.         }
  2404. #ifdef use_old_code
  2405.         /* 12Aug92  e  moved to eTeEdGuts() since it's also applicable when hasReturn is TRUE */
  2406.         if( start > 0
  2407.             && pT[sol-1] != RETURN
  2408.             && ( (c = pT[sol]) == RETURN || c == '\0' ) )
  2409.         { /* this line starts off with a RETURN or NUL and prev line has no RETURN */
  2410.           eTeUpdateLineStarts( hE, start - 1 );
  2411.           return TRUE; /* -1 ? */
  2412.         }
  2413.         else
  2414. #endif
  2415.         if( llen > (**hE).wrap || ( (c = pT[eol-1]) != RETURN && c != '\0' ) )
  2416.         { /* this line is longer than wrap or already doesn't end with a RETURN or NUL */
  2417.           eTeUpdateLineStarts( hE, start );
  2418.           return TRUE; /* +1 ? */
  2419.         }
  2420.         while ( start++ < bot )
  2421.             *linePtr++ += incr;
  2422.     }
  2423.     return FALSE;
  2424. }
  2425.  
  2426. /* eTeEdGuts
  2427.     handles insertion, deletion, and substitution
  2428.     If selection is active, then the text of the selection is deleted, then...
  2429.     If numChars is greater than 0, then the given text is inserted;
  2430.      else if numChars is equal to 0, then no text is inserted;
  2431.      otherwise (numChars is less than 0) AND THIS ONLY WORKS IF NO SELECTION...
  2432.        minus numChars characters to the RIGHT of the caret position are deleted.
  2433. */
  2434.  
  2435. static void eTeEdGuts( eRec **hE, Ptr textPtr, long numChars,
  2436.                              Boolean hasReturn, Boolean show, short style )
  2437. {
  2438.     register long    oldLen, offset;
  2439.     Rect            invalidRect;
  2440.     long newWrite = -1; /* 11Aug92  e   was: 0 */
  2441.     char *pT;            /* 12Aug92  e  */
  2442.  
  2443.     if ( (**hE).selActive ) {
  2444.         eTeHiliteRange( hE, (**hE).selStart, (**hE).selEnd );
  2445.         (**hE).selActive = FALSE;
  2446.         offset = eTeChPosToOffset( hE, (**hE).selStart );
  2447.         oldLen = eTeChPosToOffset( hE, (**hE).selEnd ) - offset;
  2448.         if( numChars < 0 ) numChars = 0;    /*  17Aug92  e   */
  2449.         if( (**hE).selEnd.v != (**hE).selStart.v )
  2450.             hasReturn = TRUE; /* 18May92  e   Oops! */
  2451.         else                  /*  5Jul92  2   Oops! */
  2452.             hasReturn |= eTeHasReturns( *(**hE).hText + offset, oldLen );
  2453.         /* 9Jul92  e */
  2454.         if( PTL( (**hE).writeChPos ) > PTL( (**hE).selStart ) )
  2455.         { if( PTL( (**hE).writeChPos ) < PTL( (**hE).selEnd ) )
  2456.             (**hE).writeChPos = (**hE).selStart;
  2457.           else
  2458.             newWrite = eTeChPosToOffset( hE, (**hE).writeChPos ) + numChars - oldLen;
  2459.         }
  2460.         /* */
  2461.         (**hE).caretChPos = (**hE).selEnd = (**hE).selStart;
  2462.         /* 3May92  e  broken by AppleArrows
  2463.         topLeft( invalidRect ) = eTeChPosToPoint( hE, (**hE).caretChPos );
  2464.         invalidRect.left -= 1;
  2465.         */
  2466.         eTeUpdateCaretRect( hE );
  2467.         topLeft( invalidRect ) = topLeft( (**hE).caretRect );
  2468.     }
  2469.     else {
  2470.         eTeSetCaretState( hE, CARET_OFF ); /* 23Jul92  e */
  2471.         offset = eTeChPosToOffset( hE, (**hE).caretChPos );
  2472.         /*  17Aug92  e  was: oldLen = numChars ? 0 : 1; */
  2473.         if( numChars < 0 )
  2474.         {    oldLen = - numChars;
  2475.             numChars = 0;
  2476.         }
  2477.         else oldLen = 0;
  2478.         /* 9Jul92  e */
  2479.         if( PTL( (**hE).writeChPos ) > PTL( (**hE).caretChPos ) )
  2480.           newWrite = eTeChPosToOffset( hE, (**hE).writeChPos ) + numChars - oldLen;
  2481.         /* */
  2482.         topLeft( invalidRect ) = topLeft( (**hE).caretRect );
  2483.     }
  2484.  
  2485.     /* Do substitution/insertion */
  2486.     Munger( (**hE).hText, offset, NULL, oldLen, textPtr, numChars );
  2487.     (**hE).dirty = TRUE;
  2488.     
  2489.     /* 12Aug92  e    as a result of adding line wrap, need to check if
  2490.                      preceeding line could be 'unwrapped' by this edit...
  2491.     */
  2492.     if( (**hE).caretChPos.h == 0                          /* edit is at the start of a line */
  2493.         && offset > 0                                      /* and it's not the first line */
  2494.         && ((pT=*(**hE).hText))[offset-1] != RETURN       /* and the prev line is a wrapped line */
  2495.         && ( pT[offset] == RETURN || pT[offset] == '\0' ) /* and this line starts with CR or NUL */
  2496.       )
  2497.     {    hasReturn = TRUE;
  2498.         eTeUpdateLineStarts( hE, (**hE).caretChPos.v - 1 );
  2499.         (**hE).caretChPos = eTeOffsetToChPos( hE, offset );
  2500.         eTeUpdateCaretRect( hE );
  2501.         topLeft( invalidRect ) = topLeft( (**hE).caretRect );
  2502.     }
  2503.     else if( hasReturn )
  2504.         eTeUpdateLineStarts( hE, (**hE).caretChPos.v );
  2505.     else
  2506.         hasReturn |= eTeAdjustLineStarts( hE, (**hE).caretChPos.v, numChars - oldLen );
  2507.     
  2508.     eTeUpdateRuns( hE, offset, offset + oldLen, numChars, style );
  2509.  
  2510.     invalidRect.right = (**hE).viewRect.right;        /* 21Jul92  e  moved outside of cond */
  2511.     
  2512.     /* Redraw the current line, starting at the caret */
  2513.     if ( invalidRect.left < (**hE).viewRect.right ) {
  2514.         invalidRect.bottom = invalidRect.top + (**hE).vScale;
  2515.         eTePrepare( hE );
  2516.         ++invalidRect.left;  /* move up here 23Jul92  e  */
  2517.         EraseRect( &invalidRect );
  2518.         /* ++invalidRect.left; !?  22Jul92  e  */
  2519.         eTeDrawLine( hE, (**hE).caretChPos, topLeft( invalidRect ) );
  2520.     }
  2521.  
  2522.     /* if RETURN added, redraw from the next line to the end of the screen */
  2523.     if ( hasReturn ) {
  2524.         invalidRect.top += (**hE).vScale;
  2525.         /* this left ghost on left edge when xor caret used...
  2526.         invalidRect.left = (**hE).leftMargin - (**hE).hOrigin;
  2527.         23Jul92  e  -- instead... */
  2528.         invalidRect.left =  (**hE).viewRect.left;
  2529.         invalidRect.bottom = (**hE).viewRect.bottom;
  2530.         /* eTePrepare( hE );  -- done by eTeDraw  21Jul92  e  */
  2531.         eTeDraw( hE, &invalidRect );
  2532.         (**hE).caretChPos = eTeOffsetToChPos( hE, offset + numChars );
  2533.         eTeUpdateCaretRect( hE );
  2534.     }
  2535.     /* Otherwise, just adjust the caret point by moving it over the inserted text */
  2536.     else {
  2537.          eTePrepare( hE );
  2538.          eTePrepareStyle( hE, style );
  2539.          (**hE).caretChPos.h += numChars;
  2540.         for ( ; numChars > 0 ; ++textPtr, --numChars ) {
  2541.             if ( *textPtr == TAB ) {
  2542.                 (**hE).caretRect.right = eTeTabStop( hE, (**hE).caretRect.right );
  2543.             }
  2544.             else
  2545.                 (**hE).caretRect.right += CharWidth( *textPtr );
  2546.         }
  2547.         (**hE).caretRect.left = (**hE).caretRect.right - 1;
  2548.     }
  2549.     /* 9Jul92  e */
  2550.     if( newWrite >= 0 ) /* 11Aug92  e   was: != 0 */
  2551.       (**hE).writeChPos = eTeOffsetToChPos( hE, newWrite );
  2552.     /* */
  2553.     if( show ) eTeShowCaret( hE );
  2554.     (**hE).maxRight = (**hE).caretRect.right;    /*  02Oct92  e  */
  2555. }
  2556.  
  2557. static short eTeChPosVToPointV( eRec **hE, short v )
  2558. {
  2559.     register long tmp;
  2560.  
  2561.     tmp = (long )v * (**hE).vScale + (**hE).topMargin - (**hE).vOrigin;
  2562.     if( tmp < -32000 )
  2563.         return -32000;
  2564.     if( tmp > 32000 )
  2565.         return 32000;
  2566.     return (short )tmp;
  2567. }
  2568.  
  2569. Point eTeChPosToPoint( eRec **hE, register ChPos aPos )
  2570. {
  2571.     register char *charPtr, c;
  2572.     register short left;
  2573.     long     offset, instyle;
  2574.     short     run;
  2575.     Point    pt;
  2576.  
  2577.     SignedByte hdlState;
  2578.  
  2579.     hdlState = HGetState( (**hE).hText );
  2580.     HLock( (**hE).hText );
  2581.  
  2582.     eTePrepare( hE );
  2583.     offset = *( *(**hE).hLines + aPos.v );
  2584.     charPtr = *(**hE).hText + offset;
  2585.     run = eTeOffsetToRun( hE, offset );
  2586.     left = (**hE).leftMargin - (**hE).hOrigin;
  2587.     do
  2588.     {    eTePrepareRun( hE, run );
  2589.         instyle = (*(**hE).hRuns)[++run] - offset;
  2590.         offset += instyle;
  2591.         while ( instyle-- && aPos.h-- )
  2592.         {    c = *charPtr++;
  2593.             if ( c == TAB )
  2594.                 left = eTeTabStop( hE, left );
  2595.             else
  2596.                 left += CharWidth( c );
  2597.         }
  2598.     } while( aPos.h > 0 );
  2599.  
  2600.     HSetState( (**hE).hText, hdlState );
  2601.  
  2602.     pt.h = left;
  2603.  
  2604.     /* may exceed limits of sixteen bits...
  2605.     pt.v = aPos.v * (**hE).vScale + (**hE).topMargin - (**hE).vOrigin;
  2606.     */
  2607.     pt.v = eTeChPosVToPointV( hE, aPos.v);
  2608.  
  2609.     return( pt );
  2610. }
  2611.  
  2612. static ChPos eTePointHtoChPosH( eRec **hE, register Point aPt, register ChPos aPos )
  2613. {
  2614.     register char *charPtr;
  2615.     register short left, c, limit;
  2616.     SignedByte hdlState;
  2617.     long     offset, instyle;
  2618.     short     run;
  2619.  
  2620.     hdlState = HGetState( (**hE).hText );
  2621.     HLock( (**hE).hText );
  2622.  
  2623.     eTePrepare( hE );
  2624.     /* Stop when we reach the point.h given to us */
  2625.     left = (**hE).leftMargin - (**hE).hOrigin;
  2626.     offset = *( *(**hE).hLines + aPos.v );
  2627.     charPtr = *(**hE).hText + offset;
  2628.     limit = *(*(**hE).hLines + aPos.v + 1) - *(*(**hE).hLines + aPos.v);
  2629.     /* 5Jul92  e  if ( aPos.v < (**hE).bounds.v - 1 ) --limit; */
  2630.     if ( charPtr[limit-1] == RETURN ) --limit;    /* 5Jul92  e   */
  2631.     run = eTeOffsetToRun( hE, offset );
  2632.     aPos.h = 0;
  2633.     do
  2634.     {    eTePrepareRun( hE, run );
  2635.         instyle = (*(**hE).hRuns)[++run] - offset;
  2636.         offset += instyle;
  2637.         while ( instyle-- && ( aPos.h < limit ) )
  2638.         {    c = *charPtr++;
  2639.             if ( c == TAB )
  2640.                 c = eTeTabStop( hE, left );
  2641.             else
  2642.                 c = CharWidth( c ) + left;
  2643.             /* see if we have passed the point */
  2644.             if ( c >= aPt.h )
  2645.             {    /* see if the point is in the second half of the character,
  2646.                    if so the returned value points to the next character. */
  2647.                 if ( c - aPt.h <= aPt.h - left ) aPos.h++;
  2648.                 limit = -1; /* force outer loop escape */
  2649.                 break;
  2650.             }
  2651.             left = c;
  2652.             aPos.h++;
  2653.         }
  2654.     } while( aPos.h < limit );
  2655.  
  2656.     HSetState( (**hE).hText, hdlState );
  2657.     return( aPos );
  2658. }
  2659.  
  2660. ChPos eTePointToChPos( eRec **hE, register Point aPt )
  2661. {
  2662.     register ChPos aPos;
  2663.  
  2664.     /* Restrict point to selectable text */
  2665.     if ( aPt.v < (**hE).topMargin - (**hE).vOrigin ) {
  2666.         aPos.v = 0;
  2667.         aPos.h = 0;
  2668.         return( aPos );
  2669.     }
  2670.     else {
  2671.         aPos.v = ( aPt.v - (**hE).topMargin + (**hE).vOrigin ) / (**hE).vScale;
  2672.         /* Past bottom of text? */
  2673.         if ( aPos.v > (**hE).bounds.v - 1 ) {
  2674.             aPos.v = (**hE).bounds.v - 1;
  2675.             aPos.h = *(*(**hE).hLines + aPos.v + 1) - *(*(**hE).hLines + aPos.v);
  2676.             return( aPos );
  2677.         }
  2678.     }
  2679.     return eTePointHtoChPosH( hE, aPt, aPos );
  2680. }
  2681.  
  2682. ChPos eTeOffsetToChPos( eRec **hE, register long anOffset )
  2683. {
  2684.     register ChPos aPos;
  2685.     register LineRec *linePtr;
  2686.     register long numLines, count, j;
  2687.  
  2688.     if ( anOffset < 0 ) {
  2689.         aPos.h = 0;
  2690.         aPos.v = 0;
  2691.         return( aPos );
  2692.     }
  2693.  
  2694.     linePtr = *(**hE).hLines;
  2695.     count = 0;
  2696.     numLines = (**hE).bounds.v - 1;
  2697. #ifdef use_original_slow_version
  2698.     while ( anOffset >= linePtr[ count + 1 ] && count < numLines )
  2699.         ++count;
  2700.     aPos.v = count;
  2701. #else
  2702.     while ( count < numLines )    {
  2703.         j = (count + numLines - 1) >> 1;
  2704.         if( anOffset < linePtr[ j ] )
  2705.             numLines = j;
  2706.         else if( anOffset >= linePtr[ j + 1 ] )
  2707.             count = j + 1;
  2708.         else    {
  2709.             count = j;
  2710.             break;
  2711.         }
  2712.     }
  2713.     aPos.v = count;
  2714. #endif
  2715.  
  2716.     if ( anOffset > linePtr[ count + 1 ] )
  2717.         aPos.h = linePtr[ count + 1 ] - linePtr[ count ];
  2718.     else
  2719.         aPos.h = anOffset - linePtr[ count ];
  2720.     return( aPos );
  2721. }
  2722.  
  2723. long eTeChPosToOffset( eRec **hE, ChPos aPos )
  2724. {
  2725.     return( *(*(**hE).hLines + aPos.v) + aPos.h );
  2726. }
  2727.  
  2728. void eTeShowCaret( eRec **hE )
  2729. {
  2730.     short        deltaH, deltaV;
  2731.     register eRec *pE = *hE;
  2732.  
  2733.     deltaH = 0;
  2734.     deltaV = 0;
  2735.  
  2736.     /* 30Apr92  e  -- was...
  2737.     if ( (*pE).caretRect.bottom > (*pE).viewRect.bottom )
  2738.         deltaV = ( (*pE).caretRect.bottom - (*pE).viewRect.bottom + (*pE).vScale - 1 ) / (*pE).vScale;
  2739.     else if ( (*pE).caretRect.top < (*pE).viewRect.top ) {
  2740.         deltaV = ( (*pE).caretRect.top - (*pE).viewRect.top ) / (*pE).vScale;
  2741.     */
  2742.     if ( (*pE).caretChPos.v >= (*pE).position.v + (*pE).vSpan )
  2743.         deltaV = (*pE).caretChPos.v - (*pE).position.v - (*pE).vSpan
  2744.                 + Min( (*pE).vContext, (*pE).vSpan >> 1 );
  2745.     else if ( (*pE).caretChPos.v < (*pE).position.v )
  2746.         deltaV = (*pE).caretChPos.v - (*pE).position.v
  2747.                  - Min( Min( (*pE).vContext, (*pE).vSpan >> 1 ), (*pE).caretChPos.v );
  2748.     /* NG for tabs and variable width fonts...
  2749.     if ( (*pE).caretChPos.h >= (*pE).position.h + (*pE).hSpan )
  2750.         deltaH = (*pE).caretChPos.h - (*pE).position.h - (*pE).hSpan + 1;
  2751.     else if ( (*pE).caretChPos.h < (*pE).position.h )
  2752.         deltaH = (*pE).caretChPos.h - (*pE).position.h;
  2753.     */
  2754.     if ( (*pE).caretRect.right > (*pE).viewRect.right )
  2755.         deltaH = ( (*pE).caretRect.right - (*pE).viewRect.right + (*pE).hScale - 1 ) / (*pE).hScale;
  2756.     else if ( (*pE).caretRect.left <= (*pE).viewRect.left ) /* was: (*pE).caretRect.right  22Jul92  e  */
  2757.     {    if( (*pE).caretRect.right + (*pE).hOrigin < (*pE).width )                        /* 22Jul92  e  */
  2758.             deltaH = - (*pE).hOrigin;                                                    /* 22Jul92  e  */
  2759.         else                                                                            /* 22Jul92  e  */
  2760.             deltaH = (*pE).caretRect.right;
  2761.         deltaH = ( deltaH - (*pE).viewRect.left - (*pE).hScale + 1 ) / (*pE).hScale;
  2762.     }
  2763.     if ( deltaH || deltaV )
  2764.     {    eTeScroll( hE, deltaH, deltaV, TRUE );
  2765.         eTeAdjustScrollMax( hE );
  2766.         eTeCalibrate( hE );
  2767.     }
  2768. }
  2769.  
  2770. /* scrolling (Panorama) */
  2771.  
  2772. /* 30Apr92  e  -- eTeScroll rewritten for >32K pixel document height */
  2773.  
  2774. void eTeScroll( eRec **hE, short hDelta, short vDelta, Boolean redraw )
  2775. {
  2776.     long            hPixels;
  2777.     long            vPixels;
  2778.     Rect            tempRect;
  2779.     register eRec    *pE;
  2780.  
  2781.     hPixels = (long )hDelta * (**hE).hScale;
  2782.     vPixels = (long )vDelta * (**hE).vScale;
  2783.  
  2784. #if 0                    /* 25Jan93  e  - to get rid of annoying inappropriate validation */
  2785.     if (redraw) {
  2786.         eTePrepare( hE );
  2787.         tempRect = (**hE).viewRect;
  2788.         if( vPixels < 32767 && vPixels > -32767 )
  2789.         {    ScrollRect( &tempRect, (short )-hPixels, (short )-vPixels, gUtilRgn );
  2790.             InvalRgn( gUtilRgn );
  2791.         }
  2792.         else InvalRect( &tempRect );
  2793.     }
  2794. #else
  2795.     if (redraw)
  2796.     {    eTePrepare( hE );
  2797.         tempRect = (**hE).viewRect;
  2798.         if( vPixels < (**hE).height && vPixels > -(**hE).height )
  2799.         {    if ( ! EmptyRgn( ((WindowPeek)(**hE).macPort)->updateRgn ) )
  2800.             {    BeginUpdate( (**hE).macPort );
  2801.                 eTeUpdate( hE );
  2802.                 EndUpdate( (**hE).macPort );
  2803.             }
  2804.             ScrollRect( &tempRect, (short )-hPixels, (short )-vPixels, gUtilRgn );
  2805.             InvalRgn( gUtilRgn );
  2806.         }
  2807.         else InvalRect( &tempRect );
  2808.     }
  2809. #endif
  2810.  
  2811.     pE = *hE;
  2812.     
  2813.     (*pE).position.h += hDelta;
  2814.     (*pE).position.v += vDelta;
  2815.  
  2816.     (*pE).hOrigin += hPixels;
  2817.     (*pE).vOrigin += vPixels;
  2818.     
  2819.     (*pE).maxRight    -= hPixels;
  2820.     (*pE).caretRect.left -= hPixels;
  2821.     (*pE).caretRect.right -= hPixels;
  2822.     (*pE).caretRect.top = eTeChPosVToPointV( hE, (*pE).caretChPos.v );
  2823.     (*pE).caretRect.bottom = (*pE).caretRect.top + (*pE).caretHeight;
  2824.  
  2825.     if (redraw) {
  2826.         BeginUpdate( (*pE).macPort );
  2827.         eTeUpdate( hE );
  2828.         EndUpdate( (**hE).macPort );
  2829.     }
  2830. }
  2831.  
  2832. void eTeScrollTo( eRec **hE, ChPos aPosition, Boolean redraw )
  2833. {
  2834.     eTeScroll( hE, aPosition.h - (**hE).position.h, aPosition.v - (**hE).position.v, redraw );
  2835.     eTeAdjustScrollMax( hE );
  2836.     eTeCalibrate( hE );
  2837. }
  2838.  
  2839. static Boolean eTeAutoScroll( eRec **hE, Point mouseLoc )
  2840. {
  2841.     short        hDelta = 0;
  2842.     short        vDelta = 0;
  2843.  
  2844.     if ( mouseLoc.h < (**hE).viewRect.left ) {
  2845.         hDelta = Max( -(**hE).hStep, -(**hE).position.h );
  2846.         if (hDelta > 0) {
  2847.             hDelta = 0;
  2848.         }
  2849.     } else if ( mouseLoc.h > (**hE).viewRect.right ) {
  2850.         hDelta = Min( (**hE).hStep, (**hE).bounds.h - (**hE).position.h - (**hE).hSpan );
  2851.         if (hDelta < 0) {
  2852.             hDelta = 0;
  2853.         }
  2854.     }
  2855.     
  2856.     if ( mouseLoc.v < (**hE).viewRect.top ) {
  2857.         vDelta = Max( -(**hE).vStep, -(**hE).position.v );
  2858.         if (vDelta > 0) {
  2859.             vDelta = 0;
  2860.         }
  2861.     } else if ( mouseLoc.v >=
  2862.                     /* (**hE).viewRect.bottom */
  2863.                     (**hE).viewRect.top + (**hE).vSpan * (**hE).vScale
  2864.                      ) {
  2865.         vDelta = Min( (**hE).vStep, (**hE).bounds.v - (**hE).position.v - (**hE).vSpan );
  2866.         if (vDelta < 0) {
  2867.             vDelta = 0;
  2868.         }
  2869.     }
  2870.     
  2871.     if ( (hDelta != 0) || (vDelta != 0) ) {
  2872.         eTeScroll( hE, hDelta, vDelta, TRUE );
  2873.         eTeCalibrate( hE );
  2874.         eTePrepare( hE );
  2875.         return(TRUE);
  2876.     } else {
  2877.         return(FALSE);
  2878.     }
  2879. }
  2880.  
  2881. static void eSbPrepare( eRec **hE )
  2882. {
  2883.     SetPort( (**hE).macPort );
  2884.     ClipRect( &(**hE).macPort->portRect );
  2885. }
  2886.  
  2887. static void eTeAdjustScrollMax( eRec **hE )
  2888. {
  2889.     short            hSpan;
  2890.     short            vSpan;
  2891.     
  2892.     eSbPrepare( hE );
  2893.  
  2894.     (**hE).hSpan = hSpan = (**hE).width / (**hE).hScale;
  2895.     (**hE).vSpan = vSpan = (**hE).height / (**hE).vScale;
  2896.     
  2897.     if ( (**hE).hSBar != NULL ) {
  2898.         SetControlMaximum( (**hE).hSBar, Max( ( (**hE).bounds.h - hSpan ), (**hE).position.h ) );
  2899.     }
  2900.  
  2901.     if ( (**hE).vSBar != NULL ) {
  2902.         SetControlMaximum( (**hE).vSBar, Max( ( (**hE).bounds.v - vSpan ), (**hE).position.v ) );
  2903.     }
  2904. }
  2905.  
  2906. static void eTeCalibrate( eRec **hE )
  2907. {
  2908.     eSbPrepare( hE );
  2909.  
  2910.     if ( (**hE).hSBar != NULL ) {
  2911.         SetControlValue( (**hE).hSBar, (**hE).position.h );
  2912.     }
  2913.     if ( (**hE).vSBar != NULL ) {
  2914.         SetControlValue( (**hE).vSBar, (**hE).position.v );
  2915.     }
  2916. }
  2917.  
  2918. static short countKeys()
  2919. {    KeyMap km;
  2920.     register short count = 1;
  2921.     register short i = 4;
  2922.     register long kmi;
  2923.     
  2924.     GetKeys( km );
  2925.     while( i-- )
  2926.     {    kmi = km[i];
  2927.         while( kmi )
  2928.         {    count <<= 1;
  2929.             kmi &= (kmi-1);
  2930.         }
  2931.     }
  2932.     return count;
  2933. }
  2934.  
  2935. static void eTeDoHscroll( eRec **hE, short whichPart )
  2936. {
  2937.     register short        delta;            /* Number of pixels to scroll        */
  2938.     short                oldValue;        /* Current scroll bar setting        */
  2939.     register short        minmax;            /* Minimum or Maximum delta            */
  2940.     long                ticks;            /* Tick count at end of Delay        */
  2941.  
  2942.     switch (whichPart) {
  2943.         case kControlUpButtonPart: // inUpButton
  2944.             delta = -( (**hE).hStep * countKeys() );
  2945.             break;
  2946.         case kControlDownButtonPart: // inDownButton
  2947.             delta =  ( (**hE).hStep * countKeys() );
  2948.             break;
  2949.         case kControlPageUpPart: // inPageUp
  2950.             Delay(PAGE_DELAY, &ticks);
  2951.             delta = (**hE).hOverlap - (**hE).hSpan;
  2952.             break;
  2953.         case kControlPageDownPart: // inPageDown
  2954.             Delay(PAGE_DELAY, &ticks);
  2955.             delta = (**hE).hSpan - (**hE).hOverlap;
  2956.             break;
  2957.     }
  2958.     oldValue = GetControlValue( (**hE).hSBar );
  2959.     if (delta < 0) {
  2960.         minmax = GetControlMinimum( (**hE).hSBar ) - oldValue;
  2961.         if (delta < minmax)
  2962.             delta = minmax;
  2963.     } else {
  2964.         minmax = GetControlMaximum( (**hE).hSBar ) - oldValue;
  2965.         if (delta > minmax)
  2966.             delta = minmax;
  2967.     }
  2968.     if (delta != 0) {
  2969.         eSbPrepare( hE );
  2970.         SetControlValue( (**hE).hSBar, oldValue + delta );
  2971.         eTeScroll( hE, delta, 0, TRUE );
  2972.         eSbPrepare( hE );
  2973.     }
  2974. }
  2975.     
  2976. static void eTeDoVscroll( eRec **hE, short whichPart )
  2977. {
  2978.     register short        delta;            /* Number of pixels to scroll        */
  2979.     short                oldValue;        /* Current scroll bar setting        */
  2980.     register short        minmax;            /* Minimum or Maximum delta            */
  2981.     long                ticks;            /* Tick count at end of Delay        */
  2982.  
  2983.     switch (whichPart) {
  2984.         case kControlUpButtonPart: // inUpButton
  2985.             delta = -( (**hE).vStep * countKeys() );
  2986.             break;
  2987.         case kControlDownButtonPart: // inDownButton
  2988.             delta =  ( (**hE).vStep * countKeys() );
  2989.             break;
  2990.         case kControlPageUpPart: // inPageUp
  2991.             Delay(PAGE_DELAY, &ticks);
  2992.             delta = (**hE).vOverlap - (**hE).vSpan;
  2993.             break;
  2994.         case kControlPageDownPart: // inPageDown
  2995.             Delay(PAGE_DELAY, &ticks);
  2996.             delta = (**hE).vSpan - (**hE).vOverlap;
  2997.             break;
  2998.     }
  2999.     oldValue = GetControlValue( (**hE).vSBar );
  3000.     if (delta < 0) {
  3001.         minmax = GetControlMinimum( (**hE).vSBar ) - oldValue;
  3002.         if (delta < minmax)
  3003.             delta = minmax;
  3004.     } else {
  3005.         minmax = GetControlMaximum( (**hE).vSBar ) - oldValue;
  3006.         if (delta > minmax)
  3007.             delta = minmax;
  3008.     }
  3009.     if (delta != 0) {
  3010.         eSbPrepare( hE );
  3011.         SetControlValue( (**hE).vSBar, oldValue + delta );
  3012.         eTeScroll( hE, 0, delta, TRUE );
  3013.         eSbPrepare( hE );
  3014.     }
  3015. }
  3016.     
  3017. /* called continuously while mouse down within a fixed part of a scroll bar.
  3018.    Called by the Toolbox during the TrackControl trap. */
  3019.  
  3020. static pascal void hSBarActionProc( ControlHandle macControl, short whichPart )
  3021. {
  3022.     eRec    **hE; 
  3023.     
  3024.     if (whichPart != 0) {
  3025.         hE = (eRec **)GetControlReference( macControl );
  3026.         eTeDoHscroll( hE, whichPart );
  3027.     }
  3028. }
  3029.  
  3030. static pascal void vSBarActionProc( ControlHandle macControl, short whichPart )
  3031. {
  3032.     eRec    **hE; 
  3033.     
  3034.     if (whichPart != 0) {
  3035.         hE = (eRec **) GetControlReference( macControl );
  3036.         eTeDoVscroll( hE, whichPart );
  3037.     }
  3038. }
  3039.  
  3040. /* end of os_mac_eEdit.c */